简体   繁体   English

Java中JSONArray迭代内部的数组

[英]Array inside JSONArray iteration in java

My Json is: 我的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": "?"
                }
            }
        ]
    }
}

My code is: 我的代码是:

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);
    }
}

For inner array - ie current data, am getting values as: 对于内部数组-即当前数据,正在获取的值为:

name=current_data, value={"timestamp":"","name":"Temperature","value":"?"} name = current_data,value = {“ timestamp”:“”,“ name”:“ Temperature”,“ value”:“?”}

How can i put another inner array so that i can get values of "timestamp":"", "name":"Temperature", "value":"?" 我如何放置另一个内部数组,以便获得“ timestamp”:“”,“ name”:“ Temperature”,“ value”:“”的值?” in separate variables instead of complete JSON 在单独的变量中,而不是完整的JSON

"value" is another jason object, so you can just call "getJasonObject()" to obtain the item and then proceed with that new array as normal. “值”是另一个jason对象,因此您只需调用“ getJasonObject()”即可获得该项,然后照常使用该新数组。

Edit: I made a fail (not enough C0FFEE in my memory) and corrected thanks to the comment. 编辑:我失败了(我的内存中没有足够的C0FFEE),并由于评论而更正。

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

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

  }

Its better to use Gson to parse JSON. 最好使用Gson解析JSON。 Anyway, if you decide to follow as this is, try as : 无论如何,如果您决定照此进行,请尝试:

You have a class like this: 您有一个像这样的课程:

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

Now, change your for loop as follows: 现在,如下更改您的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();
    }
  }
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
    }
}

//nested jsonarray //嵌套的jsonarray

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

for(i:len) 为(i:len)

{ {

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

Iterato iterator=json1.iterator(); Iterato iterator = json1.iterator();

while(iterator.hasNext()) while(iterator.hasNext())

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM