JSON - 通过JSONArray迭代 [英] JSON - Iterate through JSONArray

查看:92
本文介绍了JSON - 通过JSONArray迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON文件,里面有一些数组。我想迭代文件数组并获取它们的元素及其值。

I have a JSON file with some arrays in it. I want to iterate through the file arrays and get their elements and their values.

这就是我的文件的样子:

{
"JObjects": {
    "JArray1": [
        {
            "A": "a",
            "B": "b",
            "C": "c"
        },
        {
            "A": "a1",
            "B": "b2",
            "C": "c3",
            "D": "d4"
            "E": "e5"
        },
        {
            "A": "aa",
            "B": "bb",
            "C": "cc",
            "D": "dd"
        }

    ]
}

}       

这是我走了多远:

JSONObject object = new JSONObject("json-file.json");
JSONObject getObject = object.getJSONObject("JObjects");
JSONArray getArray = getObject.getJSONArray("JArray1");

for(int i = 0; i < getArray.size(); i++)
{
      JSONObject objects = getArray.getJSONArray(i);
      //Iterate through the elements of the array i.
      //Get thier value.
      //Get the value for the first element and the value for the last element.
}

是否可以做这样的事情?

Is it possible to do something like this?

我想这样做的原因是因为文件中的数组具有不同数量的元素。

The reason I want to do it like this is because the arrays in the file have a different number of elements.

推荐答案

更改

JSONObject objects = getArray.getJSONArray(i);

JSONObject objects = getArray.getJSONObject(i);

JSONObject objects = getArray.optJSONObject(i);

取决于您正在使用的JSON-to / from-Java库。 (看起来 getJSONObject 对你有用。)

depending on which JSON-to/from-Java library you're using. (It looks like getJSONObject will work for you.)

然后,访问对象中的字符串元素 JSONObject ,按元素名称取出它们。

Then, to access the string elements in the "objects" JSONObject, get them out by element name.

String a = objects.get("A");

如果需要 JSONObject ,您可以使用静态实用程序方法 JSONObject.getNames(JSONObject)来执行此操作。

If you need the names of the elements in the JSONObject, you can use the static utility method JSONObject.getNames(JSONObject) to do so.

String[] elementNames = JSONObject.getNames(objects);




获取第一个元素的值和最后一个元素的值元素。

"Get the value for the first element and the value for the last element."

如果element指的是数组中的组件,请注意第一个组件位于索引0处,最后一个组件是索引 getArray.length() - 1

If "element" is referring to the component in the array, note that the first component is at index 0, and the last component is at index getArray.length() - 1.


我想迭代数组中的对象并获得它们的组件和它们的值。在我的例子中,第一个对象有3个组件,scond有5个,第三个有4个组件。我想迭代它们中的每一个并获得它们的组件名称和值。

I want to iterate though the objects in the array and get thier component and thier value. In my example the first object has 3 components, the scond has 5 and the third has 4 components. I want iterate though each of them and get thier component name and value.

以下代码就是这样做的。

The following code does exactly that.

import org.json.JSONArray;
import org.json.JSONObject;

public class Foo
{
  public static void main(String[] args) throws Exception
  {
    String jsonInput = "{\"JObjects\":{\"JArray1\":[{\"A\":\"a\",\"B\":\"b\",\"C\":\"c\"},{\"A\":\"a1\",\"B\":\"b2\",\"C\":\"c3\",\"D\":\"d4\",\"E\":\"e5\"},{\"A\":\"aa\",\"B\":\"bb\",\"C\":\"cc\",\"D\":\"dd\"}]}}";

    // "I want to iterate though the objects in the array..."
    JSONObject outerObject = new JSONObject(jsonInput);
    JSONObject innerObject = outerObject.getJSONObject("JObjects");
    JSONArray jsonArray = innerObject.getJSONArray("JArray1");
    for (int i = 0, size = jsonArray.length(); i < size; i++)
    {
      JSONObject objectInArray = jsonArray.getJSONObject(i);

      // "...and get thier component and thier value."
      String[] elementNames = JSONObject.getNames(objectInArray);
      System.out.printf("%d ELEMENTS IN CURRENT OBJECT:\n", elementNames.length);
      for (String elementName : elementNames)
      {
        String value = objectInArray.getString(elementName);
        System.out.printf("name=%s, value=%s\n", elementName, value);
      }
      System.out.println();
    }
  }
}
/*
OUTPUT:
3 ELEMENTS IN CURRENT OBJECT:
name=A, value=a
name=B, value=b
name=C, value=c

5 ELEMENTS IN CURRENT OBJECT:
name=D, value=d4
name=E, value=e5
name=A, value=a1
name=B, value=b2
name=C, value=c3

4 ELEMENTS IN CURRENT OBJECT:
name=D, value=dd
name=A, value=aa
name=B, value=bb
name=C, value=cc
*/

这篇关于JSON - 通过JSONArray迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆