Parsing JSON with Dynamic Key.

1,833 views
Skip to first unread message

Shapath Neupane

unread,
Mar 19, 2015, 1:08:20 PM3/19/15
to realm...@googlegroups.com
While exploring the Pocket SDK, I came across Dynamic Keys, which I find a rarity in Java JSON Development since most of these tutorials are for static keys and mapping the data to the keys. 

{
    "status": 1,
    "list": {
        "229279689": {
            "item_id": "229279689",
            "resolved_id": "229279689",
            "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland",
            "favorite": "0",
            "status": "0",
            "resolved_title": "The Massive Ryder Cup Preview",
            "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.",
            "is_article": "1",
            "has_video": "1",
            "has_image": "1",
            "word_count": "3197",
            "images": {
                "1": {
                    "item_id": "229279689",
                    "image_id": "1",
                    "src": "http://a.espncdn.com/combiner/i?img=/photo/2012/0927/grant_g_ryder_cr_640.jpg&w=640&h=360",
                    "width": "0",
                    "height": "0",
                    "credit": "Jamie Squire/Getty Images",
                    "caption": ""
                }
            },
            "videos": {
                "1": {
                    "item_id": "229279689",
                    "video_id": "1",
                    "src": "http://www.youtube.com/v/Er34PbFkVGk?version=3&hl=en_US&rel=0",
                    "width": "420",
                    "height": "315",
                    "type": "1",
                    "vid": "Er34PbFkVGk"
                }
            }
        }
    }
}

Line 3: Contains the Key which changes for every item in the JSON List. Importantly, It is key, not a value.
Line 4: Contains the same value as the json key in Line 3.
Line 5: Contains the same Key but Value is the same as Line 4.


I wanted to post this data to Realm. However I'm having troubles creating a POJO for this JSON Item.

Where do I start to parse these dynamic keys? Regex? (I really don't want to do there)


Christian Melchior

unread,
Mar 20, 2015, 12:05:27 AM3/20/15
to Shapath Neupane, realm...@googlegroups.com
Hi Shapath

Interesting API, but no JSON parser I know of would be able to parse it automatically and neither can Realm, so you probably will have to manually parse it.
Your best bet would be to manually convert the JSON using a GSON custom deserializer or map it to a JSONObject where it is easier to manipulate the structure before you copy it to Realm.

--
Christian Melchior
Senior Android Developer


--
You received this message because you are subscribed to the Google Groups "Realm Java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-java+...@googlegroups.com.
To post to this group, send email to realm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/realm-java/baffbde2-b6ec-4c10-a210-0976e166843a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



{#HS:78799520-719#}

Shapath Neupane

unread,
Mar 20, 2015, 1:07:18 AM3/20/15
to Christian Melchior, realm...@googlegroups.com
How is it possible to get all the keys in the JSON through iteration using GSON or JsonObject. I was think about parsing the JSON till the "list" value and extracting the "item_id" and pass that value to the POJO since most of it is static or most of what I need.

Possible?
Regards,
Shapath Neupane. 

Christian Melchior

unread,
Mar 20, 2015, 1:41:43 AM3/20/15
to Shapath Neupane, realm...@googlegroups.com
Iterating using GSON is only possible using a custom TypeAdapter, you can see examples of creating one here: https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/TypeAdapter.html

Iterating using JsonObject would be done something like this:

JSONObject obj = new JSONObject(json);
List<String> keys = obj.getJsonObject("list").getKeys();
foreach (String key : keys) {
realm.createObjectFromRealm(InnerObject.class, obj.getJsonObject("list").getJsonObject(key);
}

I hope that will get you started.


--
Christian Melchior
Senior Android Developer


{#HS:78799520-719#}
Reply all
Reply to author
Forward
0 new messages