如何解析JSON对象以获取数组中的数组?

时间:2014-04-01 20:15:26

标签: android arrays json

这是我的JSON输出。我想回复" id":" 9040"从说明但我遇到了麻烦

{
  "packages": [
    {
      "instructions": [
        {
      "id": "9040",
      "fn": "test.xml",
      "@type": "down",
      "fp": ""
    }
  ],
  "id": "47968",
  "time": 1396036698630,
  "priority": 0
}

] }

这是我的代码,它返回" Package" - > "标识"

         JSONObject jObject = new JSONObject(json);
         JSONArray jsonMainNode = jObject.optJSONArray("packages");
         int lengthJsonArr = jsonMainNode.length();  
         for(int i=0; i < lengthJsonArr; i++) 
         {
             /****** Get Object for each JSON node.***********/
             JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
             fileid  = Integer.parseInt(jsonChildNode.optString("id").toString());
             if (fileid > 0 )

             Log.i("JSON parse", "id = "+ fileid);
        }

非常感谢任何帮助。

谢谢

1 个答案:

答案 0 :(得分:1)

你忘记了JSONArray指令

    {
    "packages": [
        {
            "instructions": [
                {
                    "id": "9040",
                    "fn": "test.xml",
                    "@type": "down",
                    "fp": ""
                }
            ],
            "id": "47968",
            "time": 1396036698630,
            "priority": 0
        }
    ]
}

解析

JSONObject jObject = new JSONObject(json);
JSONArray jsonMainNode = jObject.optJSONArray("packages");
JSONObject jb = (JSONObject) jsonMainNode.get(0);
JSONArray instructions = jb.getJSONArray("instructions");
JSONObject jb1 =  (JSONObject) instructions.get(0);
String id = jb1.getString("id");
相关问题