6

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.

3 Answers 3

7

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.

2

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();
}
2
  • 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<Map<String,Map<String,Object>> to get intended result.
    – StaxMan
    Nov 23, 2010 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, 2010 at 18:58
1

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.