1

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!

2

1 Answer 1

3

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

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

Comments

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.