我想从下面的 json 响应中获取值。我能够从第一个键而不是另一个键对象中获取值。我想获取所有 url 的值,但没有 url 是动态的和关键的。以下是json响应:
我能够得到 body、title 和 created_date 的值
{
"17": {
"27": {
"url": "some text here 1"
},
"28": {
"url": "some text here 12"
},
"29": {
"url": "some text here 123"
},
"title": "some text goes here",
"body": "Some text goes here",
"created_date": "1395386881"
}
}
我正在尝试的代码如下:
try {
json = new JSONTokener(jsonNewsResponse).nextValue();
if (json instanceof JSONObject) {
JSONObject jsonObject = (JSONObject) json;
Iterator<?> keys = jsonObject.keys();
while (keys.hasNext()) {
nid = String.valueOf(keys.next());
JSONObject jsonObj = jsonObject.getJSONObject(nid);
attributeValue = jsonObj.getString(attributeName);
List<String> listitems = new ArrayList<String>();
Iterator<?> key = jsonObj.keys();
while (key.hasNext()) {
String fid = String.valueOf(key.next());
System.out.println("FID"+fid);
System.out.println("FID"+fid);
System.out.println("FID"+fid);
System.out.println("FID"+fid);
JSONObject jObject = jsonObj.getJSONObject(fid);
listitems.add(jObject.getString("url"));
}
attachments = listitems.toArray(new String[0]);
}
}