2

I have JSON string with dynamic elements, till now I parse it into Map:

Map map = new Gson().fromJson(jsonString, 
        new TypeToken<HashMap<String, String>>() {}.getType());

Now I need to solve thsi situation - one of these dynamic variables could be another JSON string.

Do you have some advice ho to solve it? Thanks in advance.

EDIT: JSON string example added (formatted):

{
    "key1": "val1",
    "key2": "val2",
    "key3": {
        "subkey1": [
            "subvalue1",
            "subvalue1"
        ],
        "subkey‌​2": [
            "subvalue2"
        ]
    },
    "key4": "val3"
}
10
  • And this gives an error? If so, which? Commented Aug 31, 2012 at 12:00
  • Eg: {"key1":"val1","key2":"val2","key3": {"subkey1":"subvalue1","subkey2":"subvalue2"},"key4":"val3"} but I don't a structure of this JSON, so I don't know if is there some nested json and if so, I don't know its structure. Commented Aug 31, 2012 at 12:02
  • Yes, there is an exception: Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 57 Commented Aug 31, 2012 at 12:03
  • According to this other question I found that should occur if your json-data is invalid (with a [ in that case), but off the bat I can't see anything wrong with the json you posted. Commented Aug 31, 2012 at 12:13
  • Its weird, I'll check the structure but the JSON string is generated via PHP function json_encode() (php.net/manual/en/function.json-encode.php). Thanks for now. Commented Aug 31, 2012 at 12:15

1 Answer 1

3

What you call another JSON string is just a json object. Change the Map value type to Object from String: TypeToken>

String jsonString = "{\"key1\":\"val1\",\"key2\":\"val2\",\"key3\": {\"subkey1\":\"subvalue1\",\"subkey2\":\"subvalue2\"},\"key4\":\"val3\"}";

Map<String, Object> map = new Gson().fromJson(jsonString, new TypeToken<Map<String, Object>>() {
}.getType());

The above example works with GSON 2.2.2. And sysout(map) produces

{key1=val1, key2=val2, key3={subkey1=subvalue1, subkey2=subvalue2}, key4=val3}

As a small improvement I'd suggest that you explicitly specify map type parameters, and use Map instead of HashMap for the TypeToken.

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

3 Comments

Yes, now it works, thank you. I loop through parsed Objects in Map and I am processing them - is there some way how to solve out if Object is another Map and not string value?
@user1315357 There are obviously ways to solve it. The question is what you want to do with it. You can test if a value is a Map using the instanceof parameter. For example you could create some recursive method that prints out a value if it is a string or iterates over the values if it is a Map and recursively calls itself for every one of them. If you consider this question answered, could you accept it please?
It doesn't work with this JSON: {"key1":"val1","key2":"val2","key3":{"subkey1":["subvalue1","subvalue1"],"subkey2":["subvalue2"]},"key4":"val3"} created by this PHP code (notice that subkey1 and subkey2 is an array and not a single value as before): $foo = array(); $foo["key1"] = 'val1'; $foo["key2"] = 'val2'; $foo["key3"] = array('subkey1'=>array('subvalue1','subvalue1'),'subkey2'=>array('subvalue2')); $foo["key4"] = 'val3'; var_dump(json_encode($foo)); "doesn't work" mean that this nested JSON object is not parsed and is returned as a String.

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.