简体   繁体   English

如何在 java 中将嵌套的 Json 转换为 Map

[英]How to convert nested Json to Map in java

I am trying to read nested json and convert that into a Map.我正在尝试读取嵌套的 json 并将其转换为 Map。 I am able to parse json to java objects but not able to convert to map from those java objects.我能够将 json 解析为 java 对象,但无法从这些 Z93F725A07423D11C463 对象转换为 map。

Following is the json以下是 json

"sources": [
    {
      "database": {
        "dbConnectionId": "TestDBConnectionId",
        "tableNames": [
          {
            "tableName": "TestTableName011",
            "keys": ["column010, column011"]
          },
          {
            "tableName": "TestTableName012",
            "keys": ["column020, column021"]
          }
        ]
      }
    }
  ]

Created Java object as follows:- 

public class Sources {
    private Database database;
//Getters and Setters
}

public class Database {

    private String dbConnectionId;
    private List<TableNames> tableNames;
//Getters and Setters
}

public class TableNames {

    private String tableName;
    private List<String> keys;
//Getters and Setters
}

Let's try this out by using Simple Json (org.json.simple).让我们使用 Simple Json (org.json.simple) 来试试这个。

    public static void main(String[] args) throws Exception {
        // Read from json file. 
        Object obj = new JSONParser().parse(new FileReader("test.json"));

        // You may remove the [] first from data, then prase json object.
        JSONObject jo = (JSONObject) obj;
        //System.out.println(jo);
        //System.out.println(jo.get("database"));

        // Get database object.
        JSONObject jodb = (JSONObject) jo.get("database");
        //System.out.println(jodb.get("dbConnectionId"));
        //System.out.println(jodb.get("tableNames"));

        // Get tableNames array from database object.
        JSONArray ja = (JSONArray) jodb.get("tableNames");
        //System.out.println(ja);
        Iterator itrJa = ja.iterator();
        while (itrJa.hasNext()) {
            //System.out.println(itrJa.next());

            // Prase nested database object inside tableNames array.
            JSONObject dbObj = (JSONObject) itrJa.next();
            System.out.println(dbObj);

            // Get tableName from database object
            System.out.println(dbObj.get("tableName"));

            // Get keys array from database object
            JSONArray keys = (JSONArray) dbObj.get("keys");
            //System.out.println(keys);
            Iterator itrKeys = keys.iterator();
            while (itrKeys.hasNext()) {
                // Get key strings.
                String keyStr = itrKeys.next().toString();
                String[] keyArr = keyStr.split(",");
                System.out.println(keyArr[0].trim());
                System.out.println(keyArr[1].trim());
            }
        }
    }

Library:图书馆:

    <!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
    </dependency>

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

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