简体   繁体   English

将分层JSON字符串转换为Java对象

[英]Transform hierarchical JSON String into Java Objects

I need to transform hierarchical JSON String into some structure of objects in Java. 我需要将分层JSON字符串转换为Java中的某些对象结构。 All Objects are of the same type but they can be nested unlimited number of times. 所有对象都是相同的类型,但是它们可以嵌套无数次。 I would also need to keep order. 我还需要保持秩序。 What library can do this for me automatically? 哪个图书馆可以自动为我执行此操作?

JSON String looks like this: 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"
        }
      ]
    }
  ]
}

And I want to transform it into something like this: 我想将其转换为如下形式:

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"

EDIT 1: I used JACKSON as advised, here is my code, but I face the issue that it doesn't work recursively, only creates one object and puts sub-lists into attributes. 编辑1:我按照建议使用JACKSON,这是我的代码,但是我遇到的问题是它不能递归工作,只能创建一个对象并将子列表放入属性。 But I need map of Objects. 但是我需要对象图。

    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;   
    }
}

this is an exemple : 这是一个例子:

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();
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM