将JSON对象转换为JSON ARRAY [英] converting JSON OBJECT to JSON ARRAY

查看:159
本文介绍了将JSON对象转换为JSON ARRAY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很简单,但是很挣扎.帮我解决这个问题.

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

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

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

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

I want to convert above jsonObject into JSON ARRAY like [{ "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));
  }
}

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

I am getting only values. Please help me solve this.

推荐答案

直接将JsonObject即obj放入jsonArray

Directly put JsonObject i.e. obj into jsonArray

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

最终代码

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

这篇关于将JSON对象转换为JSON ARRAY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆