2017-10-31 1 views
-1

json 객체에서 키를 가져오고 싶습니다. 나는 그것을 얻기 위해 아래의 코드를 사용하고이 부분에서 자바에서 JSONObject에서 키를 가져올 수 없습니다.

JSONParser parser = new JSONParser(); 
    try { 

       Object obj = null; 
       try { 
        obj = parser.parse(new FileReader(new File("json/customer_list.json"))); 
       } catch (org.json.simple.parser.ParseException e1) { 
        e1.printStackTrace(); 
       } 
       JSONObject jsonObject = (JSONObject) obj; 
       JSONArray listOfBranches = (JSONArray) jsonObject.get("customers"); 
       for (int i = 0; i < listOfBranches.size(); i++) { 
        System.out.println("Customer :" + listOfBranches.get(i)); 
       } 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

,

에서 System.out.println ("고객 :"+ listOfBranches.get (I)); 나는 아래의 객체를 얻고있다

, 내가 사진, 비디오 및 도면 키를 얻으려면이 JSON에서

{"Photo":{"input_required":false,"output_type":"","input_fields":[{"input_type":"INTEGER","length":"","reg_exp":"","label":"","field_type":"TEXT"},{"values":"","length":"","reg_exp":"","label":"Mode","field_type":"drop_down"},{"length":"","input_type":"INTEGER","reg_exp":"","label":"Quantity","field_type":"TEXT"}]},"video":{"input_required":true,"output_type":"","input_pattern":""},"drawing":{"input_required":true,"output_type":"PDF","input_pattern":""}} 

. 이 일을 할 생각을 나에게 제안 해 주시겠습니까? 미리 감사드립니다.

+0

일반적으로 당신이'출력 [ "사진"]'같은 것을 사용하여 JSON 값을 얻을 수 있습니다, 하지만 자바에서 작동하는지 모르겠습니다. 'listOfBranches.get ("Photo")'는 작동합니까? – numbermaniac

답변

-2

나는, 아래의 코드를 사용하여 JSON 객체에서 키를 얻을 수 있습니다

try { 

      Object obj = null; 
      try { 
       obj = parser.parse(new FileReader(new File("json/customer_list.json"))); 
      } catch (org.json.simple.parser.ParseException e1) { 
       e1.printStackTrace(); 
      } 
      JSONObject jsonObject = (JSONObject) obj; 
      JSONArray listOfBranches = (JSONArray) jsonObject.get("customers"); 
      for (int i = 0; i < listOfBranches.size(); i++) { 
       JSONObject item = (JSONObject)listOfBranches.get(i); 
       Set keys = item.keySet(); 
       Iterator a = keys.iterator(); 
       while(a.hasNext()) { 
        String key = (String)a.next(); 
        System.out.print("key : "+key); 
       } 
       System.out.println("Customer :" + listOfBranches.get(i)+"\n item "+item); 
      } 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

해피 코딩 ...

+2

답변을 이미 알고 계시는지 질문하는 이유는 무엇입니까? 여기에 질문하기 전에 검색을 시도 했습니까? –

+0

@JankiGadhiya 예 검색 한 결과 답변을 얻지 못했습니다. 그게 질문을 게시했습니다. 그리고 나는 위의 대답으로 노력했고, 나를 위해 일한다. 그래서 그것은 같은 문제를 겪고있는 나 같은 다른 사람들을 도울 것입니다. 그게 왜 대답을 올렸지. 왜 내가 대답을 알면서도 질문을 게시해야합니까? 아마도 그것은 내 시간을 낭비 할 것입니다. 그래서 나는 그것을 선호하지 않을 것이다. – Olive

관련 문제