【问题标题】:Array inside JSONArray iteration in javajava中JSONArray迭代中的数组
【发布时间】:2013-11-10 07:02:51
【问题描述】:

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

对于内部数组 - 即当前数据,获取值如下:

名称=当前数据, value={"timestamp":"","name":"Temperature","value":"?"}

如何放置另一个内部数组,以便我可以获取 “时间戳”:“”,“名称”:“温度”,“值”:“?”在单独的变量中而不是完整的 JSON 中

【问题讨论】:

  • 呃,“价值”是一张地图。从地图中提取条目。
  • 值又是json..
  • 如果上面的 JSON 列表是准确的,并且您使用了普通的 JSON 解析器,“current_data”的值将是一个 Map。对于特定的 JSON 解析器,您使用 Map 的形式将是 JSONObject。像访问任何其他地图一样访问元素。

标签: java json arrays


【解决方案1】:

最好使用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();
    }
  }

【讨论】:

  • 为什么要遍历 elementNames 而不是只访问 objectInArray.getJSONObject("current_data")??
  • @HotLicks 他/她编写了迭代,不是为“current_data”,而是为其他元素。例如,只需查找“model_id”。
  • 除了为什么要迭代 Map 元素?您可能想要特定的值,而通过您不想要的值是没有意义的。
  • @HotLicks 对不起,我没听懂你。它出什么问题了?可能是他想要所有的价值观。
  • 但是他们回来的时候很混乱。每个值都有一个含义,并且会与一个特定的程序变量相关联。仅当唯一目的是转储 JSON 内容时,迭代才有意义。
【解决方案2】:
for (String elementName : elementNames)
  {
  JSONObject jsonobject = jsonarray.getJSONObject(elementName);

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

  }

【讨论】:

  • 你没有先访问“current_data”。
【解决方案3】:

“value”是另一个 jason 对象,因此您只需调用“getJasonObject()”即可获取该项目,然后照常处理该新数组。

编辑:我失败了(我的记忆中没有足够的 C0FFEE)并感谢评论纠正。

【讨论】:

  • 其实就是一个 JSON 对象 -> JSONObject -> Map.
  • 是的,我发现COFFEE的取值范围很窄——容易下溢和上溢。
【解决方案4】:
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
    }
}

【讨论】:

  • 你至少应该解释一下你的代码是做什么的。
  • @MichałMiszczyszyn - 它是自我记录的。
【解决方案5】:

//嵌套的json数组

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)

{

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

迭代迭代器=json1.iterator();

while(iterator.hasNext())

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

}

【讨论】:

    猜你喜欢
    • 2015-01-18
    • 2017-08-08
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    相关资源
    最近更新 更多