0

I'm trying to parse JSON with GSON and I think I'm getting the hang of it. The problem I have is it seems to stop parsing after the object name.

    public static void main(String[] args) throws Exception {
    Gson gson=new Gson();
    String json = "{\"k1\":\"v1\",\"k2\":\"v2\"}";
    Map<String,String> map=new HashMap<String,String>();
    map=(Map<String,String>) gson.fromJson(json, map.getClass());
    System.out.println(map.keySet());
}

Works fine and outputs:

[k1, k2]

And I can use those keys to get the values fine which is what I want to do.

If I use the same code with this JSON Object from json.org

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

The only output I get is

[glossary]

I feel like there is something obvious I'm missing, can someone please help point me in the right direction?

Thank you.

3
  • 1
    The second JSON object is rather complex. What output are you expecting? Commented Jul 8, 2013 at 20:09
  • Your second object has exactly one key (glossary) and one value for that key (the object containing all the other stuff). Commented Jul 8, 2013 at 20:13
  • What I'm wanting to do is get the values without knowing the names of the keys. So using the second json example from above I would want to be able to do something like this pseudo-code I thought I would be able to break down that JSON object by each level programmaticly showing the value of title, then ID, etc with some sort of loop. for(int i=0;i<map.size();i++){System.out.println(map.get(/*keyname here, maybe map.get(i) or something*/));} Commented Jul 8, 2013 at 20:16

1 Answer 1

1

The JSON object (containing "glossary") is more complex than a Map. An example of a JSON Object that could be converted to a Java Map<String, String> would be:

{"key1": "value1", "key2": "value2", "key3": "value3"}

However, in the example JSON object, there is a string key whose value is JSON object, not a String, which does not convert to Map<String, String>:

{"key": {"anotherKey": "some value"}}

There is an example from the Gson homepage here. To properly de-serialize your example, you will have to use a combination of JsonParser and Gson. For example,

String jsonObject = "your example";
JsonParser parser = new JsonParser();
JsonObject obj = parser.parse(jsonObject).getAsJsonObject();
JsonObject glossary = obj.get("glossary");

etc...

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

1 Comment

Wow, thank you! That was exactly what I was looking for I just didn't quite know how to ask it. Thank you very much!

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.