Bajan
Bajan

Reputation: 694

How to parse multiple nested JSON objects/arrays with GSON?

I have been trying to wrap my head around how to parse nested objects and arrays with GSON, still stuck. How can I parse the nested items listed in the "results" array?

{
"item": {
    "results": [
        {
            "__metadata": {
                "url": "google.com",
                "type": "website"
            },
            "listed": true,
            "market": 225,
            "town": "Toronto"
        },
        {
            "__metadata": {
                "url": "twitter.com",
                "type": "website"
            },
            "listed": true,
            "market": 225,
            "town": "Calgary"
        }
    ]
}
}

How can I easily do this with GSON within Android?

Thank you!

Upvotes: 1

Views: 855

Answers (1)

Angad Tiwari
Angad Tiwari

Reputation: 1768

Android Studio

  1. Download Plugin "GsonFormat"
  2. Create your model class
  3. Open Code->Generate->Gson
  4. Paste your json click ok - if json is valid then it will convert the following json to java class (pojo)
  5. Now create Gson object

    Gson gson=new Gson();

  6. Convert Json to java object

    T obj = gson.fromJson(contents, tClass);

  7. Now use this object "obj" to get values

Upvotes: 3

Related Questions