0

Have issues while reading a JSON String that I am building from BufferedReader and trying to assign the values to POJO class.

It works when I use this format (Discovered by accident. copy paste):

String build = {\"feature\":{\"id\":\"888888\",\"name\":\"A9 Refresh Release\"}

But actually when reading from the BufferedReader, it looks like below one:-

{"feature":{"id":"888888","name":"A9 Refresh Release"}}

Here is my code:

    BufferedReader in = new BufferedReader(newInputStreamReader(connection.getInputStream()));
    StringBuilder build = new StringBuilder();
    String inputLine;
    while ((inputLine = in.readLine()) != null) {
         build.append(inputLine); }
    in.close();
ObjectMapper mapper = new ObjectMapper();
Feature wrap = mapper.readValue(build, Feature.class);

My problem isn't that I don't know how to marshall JSON string to POJO but the JSON response which I am getting from my HttpURLConnection isn't valid for the ObjectMapper.

Tags
  • 172
  • 3
  • 16
  • `{"feature":{"id":"888888","name":"A9 Refresh Release"}` is not valid `json` - try adding another `}` – Scary Wombat Nov 15 '18 at 01:23
  • Thats what I getting from response – Tags Nov 15 '18 at 01:23
  • it ain't telling you no lies – Scary Wombat Nov 15 '18 at 01:24
  • why do you think that? – Scary Wombat Nov 15 '18 at 01:25
  • Sorry I was looking at the wrong JSON – Tags Nov 15 '18 at 01:26
  • If it not JSON than how should I be building the response as a JSON String? – Tags Nov 15 '18 at 01:34
  • Not sure what you are asking, but not really related to json specifically, Strings that want to contain `"` will need to escape them – Scary Wombat Nov 15 '18 at 01:38
  • Possible duplicate of [How to convert the following JSON String to POJO](https://stackoverflow.com/questions/41499935/how-to-convert-the-following-json-string-to-pojo) – Abhinav Nov 15 '18 at 07:04
  • @Abhinav Very close but I am not building the JSON manually. Im doing a `GET` with my `HttpURLConnection`. The JSON is provided as shown in my `String`. I cant add the rest of the code since it requires authentication Key – Tags Nov 15 '18 at 15:53
  • @Tags What do you mean by _JSON response isn't valid _? Would you mind to post that (**_invalid JSON response_**)as well as a part of your question? – Abhinav Nov 15 '18 at 16:02
  • What is your Feature.class ? Does it have a 'feature' property which is an object? I assume the Feature class has only id and name properties, so you need to create a wrapper class. (If your Feature.class is actually a wrapper, then ignore this comment) – Selindek Nov 15 '18 at 16:04
  • @Abhinav the second JSON String I posted on my question I’m getting an error. The first JSON string I manually typed works. Obviously I want to use the JSON string I’m getting from my HTTP request but the response is the second String – Tags Nov 15 '18 at 16:10
  • @Selindek it is a wrapper class and it works. I just can’t get it to work with my HTTP request. The response is on my question. – Tags Nov 15 '18 at 16:13
  • What is the exception message? – Selindek Nov 15 '18 at 16:20

2 Answers2

0

Actually, you have to parse JSON string into Jackson JsonNode first.

public <T> T readValue(JsonParser jp,
              Class<T> valueType)
            throws IOException,
                   JsonParseException,
                   JsonMappingException

Method to deserialize JSON content into a non-container type (it can be an array type, however): typically a bean, array or a wrapper type (like Boolean).

Note: this method should NOT be used if the result type is a container (Collection or Map. The reason is that due to type erasure, key and value types cannot be introspected when using this method.

Eventually, there are 2 solutions to your problem as below:-

1. Quick Parsing:-

BufferedReader in = new BufferedReader(newInputStreamReader(connection.getInputStream()));
    StringBuilder build = new StringBuilder();
    String inputLine;
    while ((inputLine = in.readLine()) != null) {
         build.append(inputLine); }
         in.close();
    ObjectMapper mapper = new ObjectMapper();
    Feature wrap = mapper.readValue(mapper.readTree(build), Feature.class);

2. Low-Level Parsing:-

BufferedReader in = new BufferedReader(newInputStreamReader(connection.getInputStream()));
    StringBuilder build = new StringBuilder();
    String inputLine;
    while ((inputLine = in.readLine()) != null) {
         build.append(inputLine); }
         in.close();
    ObjectMapper mapper = new ObjectMapper();
    JsonFactory factory = mapper.getFactory();
    JsonParser parser = factory.createParser(build);
    Feature wrap = mapper.readValue(mapper.readTree(parser), Feature.class);
Abhinav
  • 530
  • 8
  • 21
  • `mapper.readTree(build)` only accepts `JsonNode`. The `build` object is a StringBuilder. Getting this error; `The method readTree(JsonParser) in the type ObjectMapper is not applicable for the arguments (StringBuilder)` – Tags Nov 15 '18 at 17:46
  • the `readValue` im using is: `public T readValue(String content, Class valueType) throws IOException, JsonParseException, JsonMappingException { return (T) _readMapAndClose(_jsonFactory.createParser(content), _typeFactory.constructType(valueType)); } ` – Tags Nov 15 '18 at 18:27
0

I found my issue. The ObjectMapper.readValue(Sting a, valyetype) as Stated only takes in strings. I was sending a StringBuilder. The StringBuilder wont format my JSON but a String will. Simple fix

Feature wrap = mapper.readValue(build.toString(), Feature.class);
Tags
  • 172
  • 3
  • 16