2

I'm a beginner Java and Gson user and have been able to apply it to my needs. I now have some JSON data that I need to parse into a spinner as follows:

{
    "lang":[
        "arabic",
        "bengali",
        "dutch-utf8",
        "eng_root",
        "english",
        "english-utf8",
        ...
     ],
     "themes":{
         "blue":{
             "chinese_ibm500":1,
             "spanish":1,
             "bengali":1,
             "japanese":1,
             "english":1,
             "russian":1,
             "french-utf8":1,
             "eng_root":1,
             "arabic":1,
             "spanish-utf8":1,
             "portuguese":1,
             ...
         },
         "green":{
             "eng_root":1,
             "engmonsoon":1,
             "english":1
             ...
         },
         "red":{
             "chinese_ibm500":1,
             "spanish":1,
             "bengali":1,
             ...
         }
    }  
}

So from this JSON I need 2 things:

1) the array under lang is dynamic as for its the languages installed on the server. How could I get all the entries?

I have a class as follows but im stuck as to what I should do after I return lang

public class ListData {

    private List<Language> lang;

    public List<Language> getLang {
        return lang;
    }

    public static class Language {
        ???
    }
}

2) after understanding 1 I might be able to figure this one out. Under themes are colors which again can be more or less {purple, orange, whatever}. I just need a list of those themes, as far as I'm concerned I don't need to know the languages for each.

Feel like this question is turning into a book. I have searched SO extensively and hate asking questions but I'm pretty stumped. Thanks in advance.

2
  • Where is your code that uses Gson? Commented May 13, 2013 at 20:45
  • You could, of course, just parse it into JSON objects, vs doing the Gson thing. Commented May 13, 2013 at 21:28

1 Answer 1

5

1) In order to get the "lang" array, just modify

private List<Language> lang;

for

private List<String> lang;

Since the elements inside "lang" array are all strings, you don't need any class Language to store those values, they'll be parsed correctly as strings. And it doesn't matter how many strings the array contains...


2) In order to parse "themes", you have to notice that it's not an array [ ], but an object { }, so you do need to parse it with some object, and the most suitable class here is a Map like this:

private Map<String, Object> themes;

Note: as you said that you don't need the data under "blue", "green", etc... you can just Object as the value type in the map, otherwise you'd need some class...

Using a Map here allows you to have an arbitrary number of themes in your JSON response.


So in summary, you just need a class like:

public class ListData {

    private List<String> lang;
    private Map<String, Object> themes;

    //getters & setters
}

and parse your JSON with:

Gson gson = new Gson();
ListData data = gson.fromJson(yourJsonString, ListData.class);

Your list of langs will be under:

data.getLang();

and your list of themes will be under:

data.getThemes().keySet();

I suggest you to take a look at Gson documentation. It's quite short and clear and you'll understand everything much better...

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

3 Comments

Thanks alot. I actually tried this before but I was getting errors about expecting array but was object or the sorts but I only had created for lang and not for themes in ListData. Actually checking my JSON response I get themes coming before lang and when I edited my ListData with the map this time it now works. So I'm assuming that GSON didn't know how to parse themes? Incredible...
As a mini question, lets say there was lang, themes, and fonts. If i just wanted lang and themes would the above answer work by itself or would I have to create an object to handle fonts and just never use it?
It doesn't matter if you have more objects in your JSON apart from lang and themes... Gson just process your class and compare the name of the class attributes with the JSON elements names... If a class attribute match the name of the element found in the JSON, it tries to pase the content of the JSON element into the class attribute... If there's some element in your JSON that doesn't match any class attribute name, Gson just ignore that element...

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.