如何在Java中解析这种类型的JSON?

时间:2015-02-16 10:50:04

标签: java json

如何从leaveInfo中获取第三个leaveTypeId和year?

{  
"statusCode":"1001",
"message":"Success",
"response":{  
  "leaveInfo":[  
     {  

        "leaveTypeId":1,
        "year":2014

     },
     {  

        "leaveTypeId":2,
        "year":2014

     },
     {  
        "leaveTypeId":3,
        "year":2014,

     },
     {  
        "leaveTypeId":4,
        "year":2014
     },
     {  
        "leaveTypeId":5,
        "year":2014
     },
     {  
        "leaveTypeId":6,
        "year":2014
     }
  ]
}
}

我这样做是为了满足我的需要

JSONObject jobj1 = new JSONObject(result);
String statusCode = jobj1.getString("statusCode");
if (statusCode.equalsIgnoreCase("1001"))
{
    String response = jobj1.getString("response");
    JSONObject jobj2 = new JSONObject(response);
    String leaveInfo = jobj2.getString("leaveInfo");
    System.out.println("leaveInfo: "+leaveInfo);
}

我不知道如何进一步,因为leaveInfo中的数组没有键值。请帮助我这方面。

1 个答案:

答案 0 :(得分:1)

使用此

JSONObject jobj1 = new JSONObject(result);
String statusCode = jobj1.getString("statusCode");
if (statusCode.equalsIgnoreCase("1001"))
{
 String response = jobj1.getString("response");
 JSONObject jobj2 = new JSONObject(response);
 JSONArray ary=jobj2.getJSONArray("leaveInfo");
 for(int i=0;i<ary.length;i++){
 JSONObject obj=ary.getJSONObject(i);
 String leaveInfo=obj.getString("leaveTypeId");
 System.out.println("leaveInfo: "+leaveInfo);
 } 

 }

我希望这会对你有所帮助:)。