如何遍历java中的json对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27720185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to iterate through json objects in java
提问by user3724559
I am trying to iterate through my json file and get required details here is my json
我正在尝试遍历我的 json 文件并获取所需的详细信息,这是我的 json
{
"000": {
"component": "c",
"determinantType": "dt",
"determinant": "d",
"header": "h",
"determinantvalue": "null"
},
"001": {
"component": "t",
"determinantType": "i",
"determinant":"ld",
"header": "D",
"determinantvalue": "null"
},
"002": {
"component": "x",
"determinantType": "id",
"determinant": "pld",
"header": "P",
"determinantValue": "null"
}}
my java code
我的Java代码
FileReader file = new FileReader("test.json");
Object obj = parser.parse(file);
System.out.println(obj);
JSONObject jsonObject = (JSONObject) obj;
JSONArray msg = (JSONArray) jsonObject.get(key);
Iterator<String> iterator = msg.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
String component = (String) jsonObject.get("component");
System.out.println("component: " + component);
As you can see in the code I am importing my json file and trying to get next elements and printing components out of it , I should also print header,determinant and determinant value as well Thank you
正如您在代码中看到的,我正在导入我的 json 文件并尝试从中获取下一个元素并打印组件,我还应该打印标题、行列式和行列式值,谢谢
采纳答案by Jon Skeet
You don't have an array - you have propertieswith names of "000" etc. An array would look like this:
您没有数组 - 您有名称为“000”等的属性。数组如下所示:
"array": [ {
"foo": "bar1",
"baz": "qux1"
}, {
"foo": "bar2",
"baz": "qux2"
}
]
Note the [ ... ]
- that's what indicates a JSON array.
请注意[ ... ]
- 这表示 JSON 数组。
You can iterate through the properties of a JSONObject
using keys()
:
您可以遍历JSONObject
using的属性keys()
:
// Unfortunately keys() just returns a raw Iterator...
Iterator keys = jsonObject.keys();
while (keys.hasNext()) {
Object key = keys.next();
JSONObject value = jsonObject.getJSONObject((String) key);
String component = value.getString("component");
System.out.println(component);
}
Or:
或者:
@SuppressWarnings("unchecked")
Iterator<String> keys = (Iterator<String>) jsonObject.keys();
while (keys.hasNext()) {
String key = keys.next();
JSONObject value = jsonObject.getJSONObject(key);
String component = value.getString("component");
System.out.println(component);
}
回答by Dhaval
try this...
尝试这个...
FileReader file = new FileReader("test.json");
Object obj = parser.parse(file);
System.out.println(obj);
JSONObject jsonObject = (JSONObject) obj;
Iterator<String> iterator = jsonObject .iterator();
for(Iterator iterator = jsonObject.keySet().iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
System.out.println(jsonObject.get(key));
}
回答by stealthjong
There's not a JSONArray
, only a few JSONObjects
. Iterate the keys of the main JSONObject
with JSONObject.keys()
.
没有JSONArray
,只有几个JSONObjects
。迭代主要的按键JSONObject
用JSONObject.keys()
。
public static final String COMPONENT = "component";
public static final String DT = "determinantType";
public static final String D = "determinant": "d";
public static final String HEADER = "header";
public static final String DV = "determinantvalue";
JSONObject jso = getItFromSomewhere();
for (Object key : jso.keys()) {
JSONObject subset = jso.getJSONObject(key);
String d = subset.getString(D);
String header = subset.getString(HEADER);
String dv = subset.getString(DV);
System.out.println(key + " " + header + " " + d + " " + dv);
}