我有一个 JSON 文件,其中包含一些数组。我想遍历文件数组并获取它们的元素和值。
这是我的文件的样子:
{
"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.length(); 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.
}
有可能做这样的事情吗?
我之所以要这样做是因为文件中的数组具有不同数量的元素。
最佳答案
改变
JSONObject objects = getArray.getJSONArray(i);
到
JSONObject objects = getArray.getJSONObject(i);
或到
JSONObject objects = getArray.optJSONObject(i);
取决于您使用的 JSON-to/from-Java 库。 (看起来 getJSONObject
对你有用。)
然后,访问“对象”中的字符串元素JSONObject
,按元素名称取出它们。
String a = objects.get("A");
如果您需要 JSONObject
中的元素名称,可以使用静态实用方法JSONObject.getNames(JSONObject)
这样做。
String[] elementNames = JSONObject.getNames(objects);
"Get the value for the first element and the value for the last element."
如果“元素”指的是数组中的组件,请注意第一个组件在索引 0 处,最后一个组件在索引 getArray.length() - 1
处.
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.
下面的代码正是这样做的。
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
*/
https://stackoverflow.com/questions/6697147/
相关文章:
c# - 为什么 DateTime.MinValue 不能在 UTC 之前的时区中序列化?
jquery - 如何使用 JQuery 将数据放入 Rails
java - 在不知道 JSON 格式的情况下用 Java 解析 JSON
json - 将 JSON 序列化为查询字符串的标准化方法?
database - Fabric.js - 如何使用自定义属性在服务器上保存 Canvas
c# - asp.net asmx Web 服务返回 xml 而不是 json
json - 从 HttpServletRequest 检索 JSON 对象文字