0

I want to parse array inside object through GSON.. for Example

{
  'title': 'Java Puzzlers: Traps, Pitfalls, and Corner Cases',
  'isbn': '032133678X',
  'authors':[
    {
      'id': 1,
      'name': 'Joshua Bloch'
    },
    {
      'id': 2,
      'name': 'Neal Gafter'
    }
  ]
}

I am only able to parse only object i.e. title, ISBN and got its value but i don't know how to get the value of authors? Please help ,I am using JSON parsing through GSON in android..

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
dheerajraaj
  • 124
  • 13
  • http://stackoverflow.com/questions/18421674/using-gson-to-parse-a-json-array may help you get started :) – Pphoenix Jun 27 '14 at 08:14

2 Answers2

0
ArrayList<Authors> lAuthors = new ArrayList<Authors>();
List<Authors> list = new Gson().fromJson(json, lAuthors.getClass());

for (Object a : list)
{
   System.out.println(a);
}

This will give you values in class Author's object.

public class Author{

    int id;
    String name;
    //getter setter here
}

Hope it helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • its okay if there is object inside array but what about other object which is outside Author – dheerajraaj Jun 27 '14 at 08:19
  • You can have class as per your json response structure. Check [this](http://stackoverflow.com/a/8679512/1777090) and [this](http://stackoverflow.com/questions/20379387/parsing-json-nested-array-with-gson-on-android). [this](http://stackoverflow.com/questions/19169754/parsing-nested-json-data-using-gson), too. :) @dheeraj92 – MysticMagicϡ Jun 27 '14 at 08:22
0

those are usefull links for Nasted JSON parsing examples :

Parsing JSON nested array with GSON on Android

http://www.javacreed.com/simple-gson-example/

Community
  • 1
  • 1
MBH
  • 16,271
  • 19
  • 99
  • 149