Reputation: 6680
I am getting a stream of json and trying to dump it into map using jackson. Below is the syntax of json i am getting :
M1{"id":"2345","results":"2"}
R1{"Title":"Titanic","country":"US"}
R2{"Title":"Avatar","country":"US"}
Now i have below code to parse this stream in jackson parser:
ObjectMapper mapper = new ObjectMapper();
InputStreamReader stream = new InputStreamReader(
urlConn.getInputStream(),org.apache.commons.lang.CharEncoding.UTF_8);
Map<String,Object> result = mapper.readValue(stream, new TypeReference<Map<String,Object>>() { });
I am seeing the below eror:
Unexpected character ('M' (code 77)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: java.io.InputStreamReader@1d6e3d2; line: 1, column: 2]
I want to put each of the two json results into two maps. I cant change the json structure.
can some one explain how can get it to working.
Upvotes: 0
Views: 2333
Reputation: 5547
Is Not JSON:
M1{"id":"2345","results":"2"}
R1{"Title":"Titanic","country":"US"}
R2{"Title":"Avatar","country":"US"}
Is JSON:
[{"key"="M1","id":"2345","results":"2"},
{"key"="R1","Title":"Titanic","country":"US"},
{"key"="R2","Title":"Avatar","country":"US"}]
I'm not sure if that exactly matches what your data is supposed to be, but that's how you would represent an array of objects. If you're going to be using a JSON parser, you need to make sure that what you are parsing is actually JSON.
Upvotes: 2