我的回复是这样的:
{
"songs":{
"2562862600":{"id":"2562862600""pos":1},
"2562862620":{"id":"2562862620""pos":1},
"2562862604":{"id":"2562862604""pos":1},
"2573433638":{"id":"2573433638""pos":1}
}
}
下面是我的代码:
List<NameValuePair> param = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url, "GET", param);
JSONObject songs= json.getJSONObject("songs");
如何将"songs"
转换为JSONArray?
发布于 2014-03-27 20:36:17
如下所示:
JSONObject songs= json.getJSONObject("songs");
Iterator x = songs.keys();
JSONArray jsonArray = new JSONArray();
while (x.hasNext()){
String key = (String) x.next();
jsonArray.put(songs.get(key));
}
发布于 2018-04-04 22:02:32
甚至更短,并使用json函数:
JSONObject songsObject = json.getJSONObject("songs");
JSONArray songsArray = songsObject.toJSONArray(songsObject.names());
发布于 2014-03-27 20:59:28
您的响应应该是这样的,才能被限定为Json Array。
{
"songs":[
{"2562862600": {"id":"2562862600", "pos":1}},
{"2562862620": {"id":"2562862620", "pos":1}},
{"2562862604": {"id":"2562862604", "pos":1}},
{"2573433638": {"id":"2573433638", "pos":1}}
]
}
您可以按如下方式解析响应
String resp = ...//String output from your source
JSONObject ob = new JSONObject(resp);
JSONArray arr = ob.getJSONArray("songs");
for(int i=0; i<arr.length(); i++){
JSONObject o = arr.getJSONObject(i);
System.out.println(o);
}
https://stackoverflow.com/questions/22687771
复制相似问题