【发布时间】:2015-02-27 11:42:31
【问题描述】:
我正在尝试遍历我的 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"
}}
我的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);
正如您在代码中看到的那样,我正在导入我的 json 文件并尝试从中获取下一个元素并打印组件,我还应该打印标题、行列式和行列式值 谢谢
【问题讨论】:
-
您遇到错误了吗?这段代码有什么问题?
-
据我所知,您没有任何 json 数组 - 我们不知道
key是什么。 -
JSONArray msg = (JSONArray) jsonObject.get(key); “我在这里尝试获取所有密钥,即'000','001','002'”以便我可以使用它进行迭代,但我无法获取密钥
-
@user3724559 您的文件中没有 JSONArray。只有一个对象具有多个具有多个属性的对象。
-
@user3724559:我是说你不应该使用 JSONArray,因为你的 JSON 中没有任何数组。这与说您无法迭代以获取所有元素不同 - 请参阅我的答案。