Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将JSONObject转换为JSONArray?_Java_Arrays_Json - Fatal编程技术网

Java 如何将JSONObject转换为JSONArray?

Java 如何将JSONObject转换为JSONArray?,java,arrays,json,Java,Arrays,Json,我有这样的回应: { "songs":{ "2562862600":{"id":"2562862600""pos":1}, "2562862620":{"id":"2562862620""pos":1}, "2562862604":{"id":"2562862604""pos":1}, "2573433638":{"id":"2573433638""pos":1} } } JSONObject

我有这样的回应:

{
 "songs":{
          "2562862600":{"id":"2562862600""pos":1},
          "2562862620":{"id":"2562862620""pos":1},
          "2562862604":{"id":"2562862604""pos":1},
          "2573433638":{"id":"2573433638""pos":1}
         }
 }
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));
}
这是我的密码:

List<NameValuePair> param = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url, "GET", param);

JSONObject songs= json.getJSONObject("songs");
List param=new ArrayList();
JSONObject json=jParser.makeHttpRequest(url,“GET”,param);
JSONObject songs=json.getJSONObject(“songs”);
如何将
“歌曲”
转换为JSONArray

类似这样:

{
 "songs":{
          "2562862600":{"id":"2562862600""pos":1},
          "2562862620":{"id":"2562862620""pos":1},
          "2562862604":{"id":"2562862604""pos":1},
          "2573433638":{"id":"2573433638""pos":1}
         }
 }
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));
}

您的响应应该是这样的,才能限定为Json数组

{
  "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);  
}
String resp=…//源代码的字符串输出
JSONObject ob=新JSONObject(resp);
JSONArray arr=ob.getJSONArray(“歌曲”);

对于(int i=0;i要反序列化响应,需要使用HashMap

String resp = ...//String output from your source

Gson gson = new GsonBuilder().create();
gson.fromJson(resp,TheResponse.class);

class TheResponse{
 HashMap<String,Song> songs;
}

class Song{
  String id;
  String pos;
}
String resp=…//源代码的字符串输出
Gson Gson=new GsonBuilder().create();
fromJson(resp,响应类);
类响应{
HashMap歌曲;
}
班歌{
字符串id;
字符串位置;
}

甚至更短,并带有json函数:

JSONObject songsObject = json.getJSONObject("songs");
JSONArray songsArray = songsObject.toJSONArray(songsObject.names());
使用JSONArray进行直接转换

JSONArray array = (JSONArray)songs.get("songs");