如何修复“通过JsonPath获取JsonObject时,使空值虚假”

时间:2019-10-10 09:24:53

标签: java

我已经使用JsonPath通过关键字从Json String获取JsonObject。但是某些Json字符串缺少空值。请帮助我

我的Json字符串

String jsonText = \"response": {"account_type": "phone","active_orders": 73,"avatar_url": null,"birthday": 1244134800,"bookcare_amount": 0,"cart_item_count": 0,}

然后函数获取JsonObject

public JsonObject getJsonString(String json, String key) {
        try {
            Configuration config = Configuration.defaultConfiguration().jsonProvider(new GsonJsonProvider())
                    .mappingProvider(new GsonMappingProvider());
            JsonObject value = JsonPath.using(config).parse(json).read(key, JsonObject.class);
            return value;
        } catch (Throwable ex) {
            String message = "No value at JSON path \"" + key + "\"";
            throw new AssertionError(message, ex);
        }
    }

运行功能

getJsonString (jsonText, "response");

实际结果:

{
  "account_type": "phone",
  "active_orders": 73,
  "birthday": 1244134800,
  "bookcare_amount": 0,
  "cart_item_count": 0
}

===>缺少“ avatar_url”:null,

预期:

{
  "account_type": "phone",
  "active_orders": 73,
  "avatar_url": null,
  "birthday": 1244134800,
  "bookcare_amount": 0,
  "cart_item_count": 0
}

0 个答案:

没有答案