如何解析Java中的JSON值?

时间:2014-03-13 13:34:38

标签: java json

我有这样的JSON:

{
    "results": [
        {
            "updated": false,
            "notification": false,
            "id": 123456,
            "mwb": {
                "id": 15989,
                "mwb": "59595959",
                "pieces": 0,
                "origin": "RO",
                "destination": "RU",
                "status": "SOLVED",
                "bbs": null
            },

我试图通过这种方式从结果中获取某些值(例如id):

String shipmentStatus = shipmentData.getJSONObject("results").getString("id"); 

但它引发了一场沮丧。我该如何轻松地解析这个JSON?谢谢你的任何建议。


异常错误是:

03-13 14:20:04.773: W/System.err(27604): at org.json.JSON.typeMismatch(JSON.java:100)     
03-13 14:20:04.773: W/System.err(27604): at org.json.JSONObject.getJSONObject(JSONObject.java:573) –

2 个答案:

答案 0 :(得分:3)

尝试以下内容:

String shipmentStatus = ((JSONObject)
    ((JSONArray)shipmentData
        .getJSONArray("results"))
            .get(0))
            .getString("id"); 

答案 1 :(得分:0)

 JsonObject _obj  = new JsonObject("Your result String");
 JsonArray _jarray = _obj.getjsonarray(results);
 for(int i = 0; i<_jarray.length; i++){
 jsonobject obj = _jarray.getjsonobject(i);
 String _updated = obj.getstring("updated");
  String _notification= obj.getstring("notification");
  String _id= obj.getstring("id");


 }