将JSON OBJECT转换为JSON ARRAY

use*_*770 1 java json

这是非常简单的但是很难实现.帮我解决这个问题.

我有一个json数据{"abc":"test","bed":"cot","help":"me"}

我想将上面的jsonObject转换为JSON ARRAY,如[{"abc":"test","bed":"cot","help":"me"}]

JSONObject obj= new JSONObject(str.toString());
Iterator x = obj.keys();
JSONArray jsonArray = new JSONArray();
Map<String, String> map = new HashMap<String, String>();

while (x.hasNext()) {
  for(int i=0;i<obj.length();i++) {
    LOG.info("=============");
    String key = (String) x.next();
    jsonArray.put(obj.get(key));
  }
}
Run Code Online (Sandbox Code Playgroud)

我只得到价值观.请帮我解决这个问题.

Peh*_*laj 6

直接将JsonObject即obj放入jsonArray

jsonArray.put(obj);
//result is [{ "abc":"test","bed":"cot","help":"me"}]
Run Code Online (Sandbox Code Playgroud)

最终代码

JSONObject obj= new JSONObject(str);
JSONArray jsonArray = new JSONArray();
//simply put obj into jsonArray
jsonArray.put(obj);
//result is [{ "abc":"test","bed":"cot","help":"me"}]
Run Code Online (Sandbox Code Playgroud)