首页 > 解决方案 > 将分层 JSON 字符串转换为 Java 对象

问题描述

我需要将分层 JSON 字符串转换为 Java 中的某种对象结构。所有对象都属于同一类型,但它们可以无限次嵌套。我还需要保持秩序。什么图书馆可以自动为我做到这一点?

JSON 字符串如下所示:

  {
  "type": "group",
  "id": "3b7034d4-a336-4ed5-bef2-363d7a48c55e",
  "name": "group1.0",
  "pid": "null",
  "models": [
    {
      "type": "model",
      "id": "aaa01dd3-abe4-4d50-a69e-62b04199b7c5",
      "name": "analysis2",
      "pid": "3b7034d4-a336-4ed5-bef2-363d7a48c55e"
    },
    {
      "type": "model",
      "id": "aaa01dd3-abe4-4d50-a69e-62b04199b7c5",
      "name": "analysis2",
      "pid": "3b7034d4-a336-4ed5-bef2-363d7a48c55e"
    },
    {
      "type": "group",
      "id": "12704d4a-a840-433a-bcf1-eb6a32af2e0d",
      "name": "group 1.2",
      "pid": "3b7034d4-a336-4ed5-bef2-363d7a48c55e",
      "groups": [
        {
          "type": "group",
          "id": "b82b479f-18ce-4eca-a464-2e17f37238a0",
          "name": "group 1.2.1",
          "pid": "12704d4a-a840-433a-bcf1-eb6a32af2e0d"
        },
        {
          "type": "group",
          "id": "3c36995a-2f75-4805-bfa8-09fdf92bc6ec",
          "name": "1.2.1.1",
          "pid": "b82b479f-18ce-4eca-a464-2e17f37238a0"
        }
      ]
    }
  ]
}

我想把它变成这样的东西:

item[1].id == "3b7034d4-a336-4ed5-bef2-363d7a48c55e"
item[1].name== "group1.0"
item[1].depth == "1"

...

item[6].id == "3c36995a-2f75-4805-bfa8-09fdf92bc6ec"
item[6].name== "1.2.1.1"
item[6].depth == "4"

编辑1: 我按照建议使用了JACKSON,这是我的代码,但我面临的问题是它不能递归工作,只创建一个对象并将子列表放入属性中。但我需要对象图。

    try {
    Map<String,Item> map = new HashMap<String,Item>();
    ObjectMapper mapper = new ObjectMapper();

    map = mapper.readValue(json, new TypeReference<HashMap>(){});

     Iterator it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry)it.next();
            System.out.println(pair.getKey() + " = " + pair.getValue());
            it.remove(); // avoids a ConcurrentModificationException
        }           
} catch (IOException e) {
    e.printStackTrace();

    public static class Item{
    public String type;
    public String id;
    public String name;
    public String pid;
    public List<Item>  models;
    public List<Item> groups;

    void Item(String ptype, String xid, String pname,String ppid, List<Item> pmodels,List<Item> pgroups) {
        type = ptype;
        id = xid;
        name = pname;
        pid = ppid;
        models = pmodels;
        groups = pgroups;   
    }
}

标签: javajsonparsingjackson

解决方案


这是一个例子:

String jsonString = <your jsonString>;


JSONArray topArray = null; 
try {
     // Getting your top array

     // THIS IS NOT NEEDED ANYMORE 
     //topArray = json.getJSONArray(jsonString);

     //use this instead
     topArray = new JSONArray(jsonString);

      // looping through All elements
      for(int i = 0; i < topArray.length(); i++){
      JSONObject c = topArray.getJSONObject(i);

      //list holding row data
      List<NodePOJO> nodeList = new ArrayList<NodePOJO>(); 

      // Storing each json item in variable
      String nodeName = c.getString("nodeName");
      String nodeID = c.getString("nodeID");

      NodePOJO pojo = new NodePOJO();
      pojo.setNodeName(nodeName);
      //add rest of the json data to NodePOJO class

      //the object to list
      nodeList.add(pojo);

    }
} catch (JSONException e) {
        e.printStackTrace();
 }

推荐阅读