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

时间:2018-10-05 07:42:42

标签: java json parsing jackson

我需要将分层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;   
    }
}

1 个答案:

答案 0 :(得分:0)

这是一个例子:

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