Reading JSon String with Gson?

An associative array in PHP translates to a Map in Java. So, in Gson's eyes, your JSON is in format of Map.

Up vote 3 down vote favorite 2 share g+ share fb share tw.

I've been pursing around Google trying to figure this out, but I can't seem to do it. I have the following json string that is returned to my java applet from another source that I need to interact with. { "A01": {"Status": "Ready", "Time": "00:00"}, "A02": {"Status": "Ready", "Time": "00:00"}, ...... } At the moment I'm not sure how I should use Gson to parse that into my applet.

When I talked to the designers of that program. The json string was designed for use in php not java so when I decoded it in php it gave me a good multi-dimensional assoc array. Any suggestions on this.

Java json gson link|improve this question edited Nov 23 '10 at 14:07BalusC262k26271481 asked Nov 23 '10 at 13:24Pyromanci307.

An associative array in PHP translates to a Map in Java. So, in Gson's eyes, your JSON is in format of Map where the Event class has the fields status and time. Public class Event { private String Status; private String Time; // Add/generate getters, setters and other boilerplate.

} Yes, capitalized field names are ugly, but that's how your JSON look like. You would otherwise need to create a custom Gson deserializer. Here's how you can convert it.

Map events = new Gson(). FromJson(json, new TypeToken>(){}.getType()); A01, A02, etc become Map keys and its value becomes the Event value of Map. You may want to add another custom deserializer to get the Time into a java.util.Date.

As a different alternative, you can also use Map> instead.

If your json was slightly different, like below: { {"Status": "Ready", "Time": "00:00"}, {"Status": "Ready", "Time": "00:00"}, ...... } Gson would be able to convert the json into a collection of objects, the object you would have to define yourself. So you would need: public class myAClass { public String Status; public Double time; //this could be a string/or even a Date I guess, //not sure what data you are expecting //and the colon may cause a problem if parsed as a double } And then use that like so: Type listType = new TypeToken>() {}.getType(); List myAClassList = new Gson(). FromJson(json, listType); //where json is yr json string You could then use the list of objects as you require.

(further reading over here).

That link pointed me to something I wasn't even looking at. I also forgot to mention in my post that Knowing the A01, A02, etc is very important. But a link in the post that you pointed me to lead me to come up with this which works for me.

JsonParser parse = new JsonParser(); JsonObject jobj = (JsonObject)parse. Parse(status); Set> map = jobj.entrySet(); Iterator> iterator = map.iterator(); int size = map.size(); for( int k = 0; k entry = iterator.next(); String key = entry .getKey(); JsonObject jele = (JsonObject)entry.getValue(); }.

Most JSON libraries have one or two line operations to do the same; there should not be any need to manually write this code. With Gson I think you would need to pass TypeToken> to get intended result. – StaxMan Nov 23 '10 at 20:31 While I understand that, I normally have people who work with my that while they can do some programing it's not there job.

And breaking things out like that, while it is more code. It's easier for them when they look at it. – Pyromanci Nov 24 '10 at 18:58.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions