【发布时间】:2014-11-03 17:32:39
【问题描述】:
我对 Java 中的 JSON 解析相当陌生,但是当我尝试解析这个 JSON 字符串并发现它是“ID”时,它会重复两次。
[
{"id":"{ID1}","time":123},
{"id":"{ID2}","time":124}
]
这是我的 Java 代码:
// v = json string, c = "id"
String output = v.replace("[", "").replace("]", "");
JSONObject obj = new JSONObject(output);
ArrayList<String> list = new ArrayList<String>();
for(int i = 0 ; i < obj.length(); i++){
System.out.println(obj.getString(c));
list.add(obj.getString(c));
}
return list.get(1);
它返回 ID1 两次或更多次。请帮忙
【问题讨论】:
-
json是一个数组,不要去掉数组符号(即'['和']')
-
所以保持原样?
-
您在循环中的哪个位置使用循环索引
i?怎么知道要引用哪个数组元素? -
@DwB 不会给我“A JSONObject 文本必须以'{'开头”错误吗?
-
当
[]是 JSON 数组的标识时,为什么要删除它们?