Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading JSon String with Gson

Tags:

java

json

gson

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.

like image 773
Pyromanci Avatar asked Nov 23 '10 13:11

Pyromanci


3 Answers

An associative array in PHP translates to a Map in Java. So, in Gson's eyes, your JSON is in format of Map<String, Event> 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<String, Event> events = new Gson().fromJson(json, new TypeToken<Map<String, Event>>(){}.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<String, Map<String, String>> instead.

like image 153
BalusC Avatar answered Sep 20 '22 01:09

BalusC


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.Entry<String, JsonElement>> map = jobj.entrySet();
Iterator<Map.Entry<String, JsonElement>> iterator = map.iterator();
int size = map.size();
for( int k = 0; k < size; k++ )
{
    Map.Entry<String, JsonElement> entry = iterator.next();
    String key = entry .getKey();
    JsonObject jele = (JsonObject)entry.getValue();
}
like image 40
Pyromanci Avatar answered Sep 18 '22 01:09

Pyromanci


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<List<myAClass>>() {}.getType();
List<myAClass> 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)

like image 20
NimChimpsky Avatar answered Sep 20 '22 01:09

NimChimpsky