2

I' m developing an Android REST client. We use JSON as data exchange format, so I use a Jackson parser. I get different Json responses from the server like simple arrays:

{"user_id":"332","user_role":"1"} 

or something else. All these stuff I parse to LinkedHashMap<String, Object> and everything works perfectly but when I got this response from the server:

[ { "user_id":"352",
    "user_role":"expert",
    "name":"Test 12-18",
    "description":"Test" },

  { "user_id":"263",
    "user_role":"novice lab",
    "name":"Tom's Desk",
    "description":"Desk"}
]

I got null: {} after parsing.Here is my code where i use Jackson:

 ObjectMapper mapParametersToJSON = new ObjectMapper();
 String serverResponseBody = responseFromServer.getBody();
LinkedHashMap<String, Object> resultofOperation = new LinkedHashMap<String,
     Object>();
TypeReference<LinkedHashMap<String,Object>> genericTypeReferenceInformation = new
    TypeReference<LinkedHashMap<String,Object>>() {};
    try {
     resultofOperation =  mapParametersToJSON.readValue(serverResponseBody,
         genericTypeReferenceInformation);

So, why Jackson failed to parse this? How can I fix this?

1
  • 1
    why u are not using default Json Parser Api? Commented Mar 12, 2013 at 16:22

4 Answers 4

5

Others have suggested the problem, but solutions are bit incomplete. If you need to deal with JSON Objects and Arrays, you can either bind to java.lang.Object, check the type:

Object stuff = objectMapper.readValue(json, Object.class);

and you will get either List or Map (specifically, ArrayList or LinkedHashMap, by default; these defaults can be changed).

Or you can do JSON trees with JsonNode:

JsonNode root = objectMapper.readTree(json);
if (root.isObject()) { // JSON Object
} else if (root.isArray()) { ...
}

latter is often more convenient.

One nice thing is that you can still create regular POJOs out of these, for example:

if (root.isObject()) { MyObject ob = objectMapper.treeToValue(MyObject.class); } // or with Object, use objectMapper.convertValue(ob, MyObject.class)

so you can even have different handling for different types; go back and forth different representations.

Sign up to request clarification or add additional context in comments.

Comments

4

The first JSON in your question is a map, or an object. The second is an array. You're not parsing an array, you're parsing a map.

You need to do something like this:

List<MyClass> myObjects = mapper.readValue(jsonInput, new TypeReference<List<MyClass>>(){});

Almost identical question with answer here.

4 Comments

Ok.But is there any way to determine JSON type: object or array in order I can use some if/else when will write mapper.readvalue ?
I haven't used Jackson in production, but it appears that JsonNode has an isArray() method. My understanding is that when you're using a mapper, you generally know what you're mapping; if not, you'd probably use a DOM.
You can use Mapper to bind to JsonNode as well: JsonNode root = mapper.readValueAsTree(source);
Sweet, thanks StaxMan. Been meaning to look into Jackson anyway.
0

In JSON the {"key": "value"} is Object and the ["this", "that"] is Array.

So, in case when you're receiving the array of objects you should use something like List<Map<Key, Value>>.

Comments

0

You are facing an error, because [] construction can't be translated into Map reference, only in List or array.

I would recommend do it something in this way:

ObjectMapper objectMapper = new ObjectMapper();

List<Map<String,String>> parsedResult = objectMapper.reader(CollectionType.construct(LinkedList.class, MapType.construct(LinkedHashMap.class, SimpleType.construct(String.class), SimpleType.construct(String.class)))).readValue(serverResponseBody);

//if you need the one result map
Map<String, String> resultMap = new LinkedHashMap<String, String>();

for (Map<String, String> map: parsedResult){
    resultMap.putAll(map);
}

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.