2016-04-01 25 views
1

JSON:私はシンプルなJSONを使用していますネストされたJSONオブジェクトの値を取得 - ジャワのsimplejson

{ 
    "issues":[ 
     { 
      "id":"120171", 
      "fields":{ 
       "name":"Story", 
        "issuetype":{ 
         "data":"inprogress" 
        } 
      } 
     }, 
     { 
      "id":"1201", 
      "fields":{ 
       "name":"plot", 
       "issuetype":{ 
        "data":"Unknown" 
       } 
      } 
    }] 
} 

を、私はJSON配列(問題)からID、名前とデータをプルしようとしています。

JSONArray ja = (JSONArray) jsonObject.get("issues"); 
for(int i=0;i<ja.size() ; i++){ 
    JSONObject tempJsonObj = (JSONObject) ja.get(i); 
    System.out.println(tempJsonObj.get("id").toString()); 
} 

私は名前とデータのデータをどのようにリタイアするのか、id値を取得できます。

答えて

0

これは、あなたが探しているものはおそらくです:

public static void main(String[] args) throws Exception { 
    JSONParser parser = new JSONParser(); 
    JSONObject jsonObject = (JSONObject) parser.parse(JSON); 
    JSONArray ja = (JSONArray) jsonObject.get("issues"); 

    for(int i=0;i<ja.size() ; i++){ 
     JSONObject tempJsonObj = (JSONObject) ja.get(i); 
     System.out.println(String.format("ID: %s", tempJsonObj.get("id").toString())); 

     JSONObject fields = (JSONObject) tempJsonObj.get("fields"); 
     JSONObject issuetype = (JSONObject) fields.get("issuetype"); 

     System.out.println(String.format("Name: %s", fields.get("name").toString())); 
     System.out.println(String.format("Data: %s", issuetype.get("data").toString())); 
    } 
} 

出力:

ID: 120171 
Name: Story 
Data: inprogress 
ID: 1201 
Name: plot 
Data: Unknown 
関連する問題