0

I have JSON data in the following format

{"createdAt": "2013-06-02T00:00:00-08:00", "sessionId": "4982b321-1a24-4295-9fce-c106362218ca", "user": 86157463, "type": "Play", "payload": {"itemId": "15869e7", "marker": 604}, "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1", "refId": "b7b4d324", "auth": "38746efc:a1681031"}

{"createdAt": "2013-06-02T00:00:00-08:00", "type": "Login", "user": 32850778, "sessionId": "a0e125a3-4e52-42a7-b35f-c9382a8a0e9b", "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.5; rv:7.0.1) Gecko/20100101 Firefox/7.0.1", "refId": "dd4d56c5", "auth": "69646ac4:a7a75b53"}

{"createdAt": "2013-06-02T00:00:24-08:00", "sessionId": "db7d0113-b688-4737-b3b9-5da92e3de23a", "user": 95035757, "type": "Recommendations", "payload": {"recommendedItems": ["10611", "9722e23", "31396", "28828", "20662", "17342", "21070", "28415", "18273", "27930", "35266", "28895", "11108", "12121", "16045", "12203", "11423", "13363e27", "21304", "18724e1", "38111e66", "9232", "36851e45", "31672", "14502"]}, "userAgent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.142 Safari/535.19", "refId": "e94bc09f", "auth": "699d7b63:cec0d4e1"}

As you can see each records has varying fields in different ordering .when i use GSON to parse the above code i get a result only for the first record.I am using the following code

BufferedReader br = new BufferedReader( new FileReader("C:/Users/inhmadala/Desktop/SampleCCP.json"));
 Gson gson = new GsonBuilder().create(); 
JsonReader reader = new JsonReader(new StringReader(br.readLine()));
reader.setLenient(true); 
//convert the json string back to object 
//JsonReader.setLenient(true); 
DataObject obj = gson.fromJson(reader, DataObject.class);

Can some one advice me on what is wrong here.

2 Answers 2

1

Your data file has three objects. Based on the docs, the fromJson method serializes a single object, that's why you are only getting one object.

One alternative would be to change your data file so that it is a single valid JSON object, but make that object an array containing all the session objects

[
{createdAt...}
,
{createdAt...}
,
{createdAt...}
]

Otherwise it will be necessary to preprocess your file using some non-JSON logic (splitting/trimming text based on blank lines, etc) and call fromJson in a loop, once for each block of text representing an object.

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

Comments

0

I would create a single class that contains all possible fields coming from your source. This would assure you that you can always parse the JSON string. Then, you can check which fields are filled or not. Moreover, field ordering into the JSON string is not a problem for the parser.

Of course, to do this, you should have a finite number of possibilities, but this seems your case from the example you posted.

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.