I have a JSON file like this:
[ { "number": "3", "title": "hello_world", }, { "number": "2", "title": "hello_world", } ]
Before the files had a root element, I would use:
Wrapper w = gson.fromJson(JSONSTRING, Wrapper.class);
the code, but I can't think of how to code the Wrapper class since the root element is an array.
I tried using:
Wrapper[] wrapper = gson.fromJson(jsonLine, Wrapper[].class);
from:
public class Wrapper{ String number; String title; }
But no luck. How else can I read this using this method?
PS I got this to work with:
JsonArray entries = (JsonArray) new JsonParser().parse(jsonLine); String title = ((JsonObject)entries.get(0)).get("title");
But I would rather know how to do it (if possible) with both methods.
java json arrays gson
Edd Aug 24 '13 at 18:17 2013-08-24 18:17
source share