java中JSONArray迭代内的数组

时间:2013-10-30 15:19:39

标签: java json arrays

我的Json是:

{
    "Response": {
        "Asset": [
            {
                "id": 2461,
                "name": "TestAsset7771",
                "model_name": "TestModel777",
                "serial_number": "TestAsset7771",
                "current_data": {
                    "timestamp": "",
                    "name": "Temperature",
                    "value": "?"
                }
            },
            {
                "id": 2448,
                "model_id": 1229,
                "name": "TestAsset777",
                "model_name": "TestModel777",
                "serial_number": "TestAsset777",
                "current_data": {
                    "timestamp": "",
                    "name": "Temperature",
                    "value": "?"
                }
            }
        ]
    }
}

我的代码是:

JSONObject outerObject = new JSONObject(jsonObj.toString());
JSONObject innerObject = outerObject.getJSONObject("Response");
JSONArray jsonArray = innerObject.getJSONArray("Asset");
for (int i = 0, size = jsonArray.length(); i < size; i++)
{
    JSONObject objectInArray = jsonArray.getJSONObject(i);
    String[] elementNames = JSONObject.getNames(objectInArray)
    for (String elementName : elementNames)
    {
       String value = objectInArray.getString(elementName);
       System.out.printf("name=%s, value=%s\n", elementName, value);
    }
}

对于内部数组 - 即当前数据,我得到的值为:

  

名= current_data,   值= { “时间戳”: “”, “姓名”: “温度”, “值”: “?”}

我如何放置另一个内部数组,以便我可以获得值 “timestamp”:“”,“name”:“Temperature”,“value”:“?”在单独的变量而不是完整的JSON

5 个答案:

答案 0 :(得分:0)

“value”是另一个jason对象,所以你可以调用“getJasonObject()”来获取该项,然后照常继续使用该新数组。

编辑:我做了一次失败(在我的记忆中没有足够的C0FFEE)并且由于评论而更正了。

答案 1 :(得分:0)

for (String elementName : elementNames)
  {
  JSONObject jsonobject = jsonarray.getJSONObject(elementName);

    System.out.printf( "name=%s, value=%s\n",jsonobject.getString("name"),jsonobject.getString("value"));

  }

答案 2 :(得分:0)

最好使用Gson来解析JSON。无论如何,如果您决定按照这样做,请尝试:

你有一个这样的课程:

class CurrentData{
  String name,timestamp,value;
  void print(){
    System.out.printf("name=%s, timestamp=%s, value=%s\n", name,timestamp, value);
  }
}

现在,按如下方式更改for循环:

 for (String elementName : elementNames)
  {
    if(!elementName.equals("current_data")){
    String value = objectInArray.getString(elementName);
    System.out.printf("name=%s, value=%s\n", elementName, value);
    }
    else{
    CurrentData obj=new CurrentData();// You can use array of objects declaring outside the loop as your need
    JSONObject curr_object=objectInArray.getJSONObject("current_data");
    obj.name=curr_object.getString("name");
    obj.timestamp=curr_object.getString("timestamp");
    obj.value=curr_object.getString("value");
    obj.print();
    }
  }

答案 3 :(得分:-1)

JSONObject outerObject = new JSONObject(jsonObj.toString());
JSONObject innerObject = outerObject.getJSONObject("Response");
JSONArray jsonArray = innerObject.getJSONArray("Asset");
for (int i = 0, size = jsonArray.length(); i < size; i++) {
    JSONObject objectInArray = jsonArray.getJSONObject(i);
    JSONObject currentData = objectInArray.getJSONObject("current_data");
    if (currentData != null) {
        String timestamp = currentData.getString("timestamp");
        String name = currentData.getString("name");
        String value = currentData.getString("value");
        // Assign above results to array elements or whatever
    }
}

答案 4 :(得分:-2)

//嵌套jsonarray

FileReader inp=new FileReader("xyz.json");
JSONParser parser=new JSONParser();
Object obj=parser.parse(inp);
JSONArray jsonArray=(JSONArray) obj;
int len=jsonArray.size();

有(ⅰ:LEN)

{

JSONArray json1 =(JSONArray)jsonArray.get(i);

Iterato iterator = json1.iterator();

而(iterator.hasNext())

的System.out.println(iterator.next());

}