115

I have a JSON file like this:

[
    {
        "number": "3",
        "title": "hello_world",
    }, {
        "number": "2",
        "title": "hello_world",
    }
]

Before when files had a root element I would use:

Wrapper w = gson.fromJson(JSONSTRING, Wrapper.class);

code but I can't think how to code the Wrapper class as the root element is an array.

I have tried using:

Wrapper[] wrapper = gson.fromJson(jsonLine, Wrapper[].class);

with:

public class Wrapper{

    String number;
    String title;

}

But haven't had any luck. How else can I read this using this method?

P.S I have got this to work using:

JsonArray entries = (JsonArray) new JsonParser().parse(jsonLine);
String title = ((JsonObject)entries.get(0)).get("title");

But I would prefer to know how to do it (if possible) with both methods.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Eduardo
  • 6,900
  • 17
  • 77
  • 121
  • 4
    Are you sure there is comma after title elements? If you remove them `Wrapper[] data = gson.fromJson(jElement, Wrapper[].class);` works fine for me. – Pshemo Aug 24 '13 at 18:45
  • 1
    That'l be the problem.. such a simple mistake! – Eduardo Aug 24 '13 at 18:46

4 Answers4

117

Problem is caused by comma at the end of (in your case each) JSON object placed in the array:

{
    "number": "...",
    "title": ".." ,  //<- see that comma?
}

If you remove them your data will become

[
    {
        "number": "3",
        "title": "hello_world"
    }, {
        "number": "2",
        "title": "hello_world"
    }
]

and

Wrapper[] data = gson.fromJson(jElement, Wrapper[].class);

should work fine.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 1
    @Snake take a look at [Google Gson - deserialize list object? (generic type)](https://stackoverflow.com/q/5554217) – Pshemo Oct 01 '18 at 08:08
  • 1
    @Snake BTW, in case of `jackson` library and [this comment](https://stackoverflow.com/questions/6349421/how-to-use-jackson-to-deserialise-an-array-of-objects#comment37099398_6349488) creating array and wrapping it with `Arrays.asList(..)` is faster than creating a list using TypeRefence. I didn't test it with `gson` library but it may be worth to benchmark it. – Pshemo Oct 01 '18 at 20:59
42
Gson gson = new Gson();
Wrapper[] arr = gson.fromJson(str, Wrapper[].class);

class Wrapper{
    int number;
    String title;       
}

Seems to work fine. But there is an extra , Comma in your string.

[
    { 
        "number" : "3",
        "title" : "hello_world"
    },
    { 
        "number" : "2",
        "title" : "hello_world"
    }
]
Lord Zed
  • 750
  • 7
  • 28
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
17
public static <T> List<T> toList(String json, Class<T> clazz) {
    if (null == json) {
        return null;
    }
    Gson gson = new Gson();
    return gson.fromJson(json, new TypeToken<T>(){}.getType());
}

sample call:

List<Specifications> objects = GsonUtils.toList(products, Specifications.class);
chenyueling
  • 171
  • 1
  • 4
2
Wrapper[] data = gson.fromJson(jElement, Wrapper[].class);