0

I am using goggle's search api to get topics id which is used to get JSON response from topic api.The returned response looks like this

{
"id":"/m/01d5g",
"property":{
    "/amusement_parks/ride_theme/rides":{...},
    "/award/ranked_item/appears_in_ranked_lists":{...},
    "/book/book_character/appears_in_book":{
            "valuetype":"object",
            "values":[
                {
                    "text":"Inferno",
                    "lang":"en",
                    "id":"/m/0g5qs3",
                    "creator":"/user/duck1123",
                    "timestamp":"2010-02-11T04:00:59.000Z"
                },
                {
                    "text":"Batman: Year One",
                    "lang":"en",
                    "id":"/m/0hzz_1h",
                    "creator":"/user/anasay",
                    "timestamp":"2012-01-25T11:05:03.000Z"
                },
                {
                    "text":"Batman: The Dark Knight Returns",
                    "lang":"en",
                    "id":"/m/0hzz_sb",
                    "creator":"/user/anasay",
                    "timestamp":"2012-01-25T11:22:17.001Z"
                },
                {
                    "text":"Batman: Son of the Demon",
                    "lang":"en",
                    "id":"/m/071l77",
                    "creator":"/user/wikimapper",
                    "timestamp":"2013-07-11T15:20:32.000Z"
                },
                {
                    "text":"Joker",
                    "lang":"en",
                    "id":"/m/04zxvhs",
                    "creator":"/user/wikimapper",
                    "timestamp":"2013-07-11T16:58:37.000Z"
                },
                {
                    "text":"Arkham Asylum: A Serious House on Serious Earth",
                    "lang":"en",
                    "id":"/m/0b7hyw",
                    "creator":"/user/wikimapper",
                    "timestamp":"2013-07-11T19:26:54.000Z"
                }
            ],
            "count":6.0
    },
    "/book/book_subject/works":{...},
    "/comic_books/comic_book_character/cover_appearances":{...},
    ... 
}
}

I want to decipher this so that i can get relevant information such as, "/book/book_character/appears_in_book" itself is a property for response and only required value that i want from it is "text" and "id" e.g. "text":"Batman: Year One" and "id":"/m/0hzz_1h". Since the response does not have fixed properties, and which may varying according to response id. how can i covert this JSON response in java Class where i can store "/book/book_character/appears_in_book" as one serialized class and containing Collection of values such has id and text and appears_in_book as name variables for class.

I considered GSON to do this. since name of property is not constant i can not use it to covert JSON to Java Object. currently i am iterating over each property by hard coding and filling them in java variables.

If some one can provide efficient way to do so i will appreciate help.

Shawn Simister
  • 4,613
  • 1
  • 26
  • 31
Yogesh
  • 4,546
  • 2
  • 32
  • 41
  • as @Shawn suggested i parsed it using json-lib-2.4 but still i have to manually extract "text" and "id" from JSONArray, which i am feeling there must be some better way to do it! – Yogesh Jul 30 '13 at 06:24

1 Answers1

1

You could do this dynamically using reflection in Java but this is an advanced feature of Java and it may make your code more complicated than it needs to be.

See: Dynamically create an object in java from a class name and set class fields by using a List with data

A simpler alternative would be to just parse the JSON into a bunch of nested Maps and Lists exactly as they're given in the JSON data.

See: How to parse JSON in Java

Community
  • 1
  • 1
Shawn Simister
  • 4,613
  • 1
  • 26
  • 31