【问题标题】:How to Convert JSON String arrray to Json Array list如何将 JSON 字符串数组转换为 Json 数组列表
【发布时间】:2013-01-25 05:17:19
【问题描述】:

我有 Json 字符串数组,看起来像这样,

{ {name:"214",value:true,Id:0},
  {name:"215",value:true,Id:0},
  {name:"216",value:true,Id:0}
}

并希望将此字符串转换为 Json 数组对象并迭代列表以读取每个对象的值。然后将值设置为相应的 dto 并保存。 但我没有找到任何将普通 JSON 数组字符串转换为 json 数组对象的好方法。

我没有使用 google json ,我希望它本身在普通 json 中完成。请帮助我

和java类我想要这样的东西

JSONObject[] jsonObjectList = String after convert();

    for (JSONObject jsonObject : jsonObjectList) {
        System.out.println(" name is --"+jsonObject.get("name"));
        System.out.println(" value is ---"+jsonObject.get("value"));
        System.out.println(" id is ----"+jsonObject.get("id"));

    }

【问题讨论】:

  • 什么是JSONObject? net.sf.*?
  • 是的,它的 net.sf.* 只有 ..
  • 那么,JSONArray 就可以了,需要确保 JSON Array String 中有括号 [ 和 ]。

标签: java json json-lib


【解决方案1】:

这里是解析 json 对象的例子.. 使用JSON lib 这个..

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
public class TestJson {
    public static void parseProfilesJson(String jsonStr) {
        try {
            JSONArray nameArray = (JSONArray) JSONSerializer.toJSON(jsonStr);
            System.out.println(nameArray.size());
            for(Object js : nameArray){
                JSONObject json = (JSONObject) js;
                System.out.println(json.get("date"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        String s = "[{\"date\":\"2012-04-23\",\"activity\":\"gym\"},{\"date\":\"2012-04-24\",\"activity\":\"walking\"}]";
        parseProfilesJson(s);
    }
}

【讨论】:

    【解决方案2】:

    使用这个: 假设您的 JSONArraynet.sf.json.JSONArray

    String str = "[ {name:\"214\",value:true,Id:0},{name:\"215\",value:true,Id:0},{name:\"216\",value:true,Id:0}]";
    JSONArray array = JSONArray.fromObject(str);
    

    【讨论】:

    • 您的 JSON 字符串需要以 [ 开头并以 ] 结尾
    【解决方案3】:

    我只是结合了一些答案并找到了正确的解决方案 这是代码..

    String str = "[ {name:\"214\",value:true,Id:1},{name:\"215\",value:false,Id:2},{name:\"216\",value:true,Id:3}]";
        JSONArray array = JSONArray.fromObject(str);
        for (Object object : array) {
            JSONObject jsonStr = (JSONObject)JSONSerializer.toJSON(object);
              System.out.println(" name is --"+jsonStr.get("name"));
                System.out.println(" value is ---"+jsonStr.get("value"));
                System.out.println(" id is ----"+jsonStr.get("Id"));
        }
    

    【讨论】:

      【解决方案4】:

      你可以这样做,

      String str = "[ {name:\"214\",value:true,Id:0},{name:\"215\",value:true,Id:0},{name:\"216\",value:true,Id:0}]";
      JsonParser parser = new JsonParser();
      JsonElement element = parser.parse(str);
      JsonArray jasonArray = element.getAsJsonArray();
      

      如果您使用的是import com.google.gson.*;

      【讨论】:

        【解决方案5】:

        如果你有 DTO 类,映射可以通过反射自动完成:

        import net.sf.json.JSONArray;
        
        List<DTO> list = (List<DTO>) JSONArray.toList(JSONArray.fromString(str), DTO.class);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-03-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-20
          • 2015-04-05
          • 2011-11-22
          相关资源
          最近更新 更多