如何使用javax.json.bind-api解析复杂的JSon字符串

时间:2019-07-02 13:12:35

标签: java json jboss javax.json

我需要解析从graphql查询返回到Mongo数据库的复杂JSon字符串。可以在执行查询的基础上对该JSon字符串进行一些更改

在我的系统上可以使用javax.json.bind API,并且我想使用该API尽可能灵活和通用地定义解析器

此JSon字符串的示例如下:

{
  "data": {
    "tbFD": {
      "uid": "74d378bb-f995-4521-8686-d08a552d87ec",
      "eanCode": "0716736038353",
      "catUid": "b00558ff-2556-437a-88cc-ac7c6e879f2a",
      "globalFId": 1,
      "cSG": [],
      "tbFV": {
        "uid": "3d96a05c-8076-4e38-9b2d-51077eef4367",
        "fSize": null,
        "bSize": 21,
        "eSize": 47,
        "otherO": "",
        "catUid": "b00558ff-2556-437a-88cc-ac7c6e879f2a",
        "tbFM": {
          "uid": "79cc8002-6501-4010-b280-5fab143a478d",
          "catUid": "b00558ff-2556-437a-88cc-ac7c6e879f2a",
          "name": "CA6645",
          "fType": "PLASTIC",
          "remoteEnabled": true,
          "tbCO": {
            "uid": "81f0c1c7-d2e4-44ed-8632-b38b77a9eba8",
            "name": "CARRERA VISTA",
            "catUid": "b00558ff-2556-437a-88cc-ac7c6e879f2a"
          },
          "tbBR": {
            "uid": "bcb0310b-1e3a-4489-b7e5-9eb94d93b201",
            "name": "CARRERA",
            "catUid": "b00558ff-2556-437a-88cc-ac7c6e879f2a"
          }
        }
      }
    }
  },
  "errors": [],
  "extensions": null
}

目前,我编写以下代码

        String key="", value="";
    Map<String,String> retValue=new HashMap<String,String>();

    //Define JSon parser
    JsonParser jsonParser = Json.createParser(new StringReader(jsonText));

    //Parse the JSon string
    while (jsonParser.hasNext()){
        JsonParser.Event event = jsonParser.next();
        switch(event) {
            case START_ARRAY:
            case END_ARRAY:
            case START_OBJECT:
            case END_OBJECT:
                break;
            case KEY_NAME:
                if (!key.equalsIgnoreCase(jsonParser.getString())){
                    if (!key.isEmpty()){
                        //Store the combination key + value
                        System.out.print("Key : " + key + " - Value : " + value);
                        retValue.put(key,value);
                        value="";
                    }
                }
                key=jsonParser.getString();
                System.out.print(event.toString() + " " + key + " - ");
                break;
            case VALUE_NULL:
                break;
            case VALUE_FALSE:
                value="false";
                System.out.println(event.toString() + " " + value);
                break;
            case VALUE_TRUE:
                value="true";
                System.out.println(event.toString() + " " + value);
                break;
            case VALUE_STRING:
            case VALUE_NUMBER:
                value=jsonParser.getString();
                System.out.println(event.toString() + " " + value);
                break;
        }
    }

但是不会返回所有字段,例如“ tbBR”的元素丢失。

我希望提取每个字段并将值存储在专用类中,以供将来在执行解析的类之外使用。

哪里出错了?

提前感谢您的任何建议

0 个答案:

没有答案