1

I have this json String

{"data":"[Level [key=LevelKey [keyEnd=0], Description=abc], Level [key=levelKey [keyEnd=1], Description=xyz]", "id":"123"}

And corresponding java classes are

public class Level {
  public LevelKey key;
  public String id;
 }


 public class LevelKey{
 public String keyEnd;
 }

I want to convert this data json string to list of Level object using Jackson

 ObjectMapper mapper = new ObjectMapper();
    List<Level> arr = mapper.readValue(data, new TypeReference<List<Level>>(){});

But I am getting below error

com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Level': was expecting ('true', 'false' or 'null')

Is there any other method to parse it?

1
  • the part of "[Level [key=LevelKey [keyEnd=0], Description=abc], Level [key=levelKey [keyEnd=1], Description=xyz]" is not a valid json but a whole string, you will have to parse it yourself i.e write a class that can extract all the values like key, keyEnd and Description to variables Commented Feb 13, 2018 at 9:22

3 Answers 3

1

The below does not look like a proper JSON for the purpose (except for a standard fixed string)

"[Level [key=LevelKey [keyEnd=0], Description=abc], Level [key=levelKey [keyEnd=1], Description=xyz]"

You could correct the data part of your JSON to something like below (Closest to your JSON in question) :

[\"Level [key=LevelKey [keyEnd=0], Description=abc]\",\" Level [key=levelKey [keyEnd=1], Description=xyz]\"]

Is there any other method to parse it?

You could use a direct class reference of ArrayList instead of having to instantiate TypeReference like below to parse the above (corrected) json string :

List<Level> arr = mapper.readValue(data, (new ArrayList<Level>()).getClass());
Sign up to request clarification or add additional context in comments.

Comments

1

This was an interesting one I must say. Take a look at code snippet I think I got it correct :

String data ="{\"data\":\"[Level [key=LevelKey [keyEnd=0], Description=abc], Level [key=levelKey [keyEnd=1], Description=xyz]\", \"id\":\"123\"}";
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
List<Level> arr = mapper.readValue(data, (new ArrayList<Level>()).getClass());
System.out.println(arr);

I got the following output :

[{data=[Level [key=LevelKey [keyEnd=0], Description=abc], Level [key=levelKey [keyEnd=1], Description=xyz], id=123}]

Also if you encountered any JsonParseException which according to documentation means :

Exception type for parsing problems, used when non-well-formed content (content that does not conform to JSON syntax as per specification) is encountered.

So while hacking the JSON you can update the ObjectMapper object like this :

mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

Also as mentioned by Exception_al using a direct class reference of ArrayList instead of having to instantiate TypeReference like below to parse the above (corrected) json string.

List<Level> arr = mapper.readValue(data, (new ArrayList<Level>()).getClass());

Hope this helped.

Comments

0

The String is not the JSON representation of what you expect you get deserialized into JAVA.
This is a JSON String:

"{"data":[{"key":{"keyEnd":0},"Description":"abc"},{"key":{"keyEnd":1},"Description":"abc"}],"id":"123"}"

So, there is either a problem with the String or you need to do the parsing yourself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.