converting JSON OBJECT to JSON ARRAY

后端 未结 2 1252
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-16 08:14

This is very simple one but struggeling. help me out of this.

I am having a json data { \"abc\":\"test\",\"bed\":\"cot\",\"help\":\"me\"}

I want to convert a

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-16 08:33

    Directly put JsonObject i.e. obj into jsonArray

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

    Final code

    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"}]
    

提交回复
热议问题