How to read single JSON field with Jackson

…衆ロ難τιáo~ 提交于 2021-01-22 10:35:09

问题


I have a fairly large JSON response in which I'm interested in single field - status:

{
  "title": "Some title",
  "status": "pending",
  "data": {
    ...
  },
  "meta": {
    ...
  }
}

All I need to do is read the status value of the JSON response as string. I would prefer to not have to build a POJO to model it, because in my application I just need to store the JSON in a database on a particular status or discard it.

The application already uses Jackson for other more complicated cases so I'd prefer to stick with that library. So far all the examples I've found try to map the JSON to an object.


回答1:


If the field required is a non-null text field, at the "first level" of the hierarchy (i.e., not any nested object) of a JSON object small enough to fit in main memory, a simple way of retrieving its value is using a method like

  public static String readField(String json, String name) throws IOException {
    if (field != null) {
      ObjectNode object = new ObjectMapper().readValue(json, ObjectNode.class);
      JsonNode node = object.get(name);
      return (node == null ? null : node.textValue());
    }
    return null;
  }

ObjectNode is a generic Jackson class, not a POJO. If multiple values are to be used, the ObjectMapper should be cached (it is even thread-safe).

Running

System.out.println(readField(response, "status"));

using the JSON response string above, returns

pending

as expected. A similar solution can be found elsewhere in StackOverflow.

For very large JSON objects (e.g., stored in files), the streaming approach of Jackson should be used, as suggested in other answers.




回答2:


If the json response is not very large, you can use ObjectMapper.readTree to deserialize the full json into JsonNode and get a particular property in it.

For example:

ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(json);
JsonNode statusNode = jsonNode.get("status");
String statusValue = statusNode.textValue();

You can also deserialize the full json into ObjectNode by ObjectMapper.readValue suggested in other answer.

If the json response is huge and deserialize the full json is a concern, you may consider to use the Jackson Streaming API, JsonParser, to parse the json.

Following is an example:

String statusValue = null;

JsonFactory jsonFactory = new JsonFactory();
try (JsonParser parser = jsonFactory.createParser(json)) {
    JsonToken startToken = parser.nextToken();
    while (parser.nextToken() != JsonToken.END_OBJECT) {
        String fieldName = parser.getCurrentName();
        if ("status".equals(fieldName)) {
            if (parser.nextToken() == JsonToken.VALUE_STRING) {
                statusValue = parser.getValueAsString();
                break;
            }
        } else {
            parser.skipChildren();
        }
    }
}

JsonParser break down the json into a sequence of tokens which you can iterate one by one. In the example, JsonParser.skipChildren is used to skip over a complete object or an array such that only the first level of json hierarchy is checked.



来源:https://stackoverflow.com/questions/38732849/how-to-read-single-json-field-with-jackson

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!