118

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.

2
  • 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. Commented Aug 24, 2013 at 18:45
  • 1
    That'l be the problem.. such a simple mistake! Commented Aug 24, 2013 at 18:46

4 Answers 4

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.

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

2 Comments

@Snake BTW, in case of jackson library and this comment 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.
43
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"
    }
]

Comments

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);

4 Comments

For me, this is turning my objects into a List of LinkedTreeMap instead of a List of Specification objects(for example).
Where did you get GsonUtils class?
GsonUtils is the class where he put his own toList() method.
This cannot work: the TypeToken must create a wrong value since T isn't known when this is compiled. That'll probably create the type token for List<Object>. You'd have to use the actual class when creating the type.
2
Wrapper[] data = gson.fromJson(jElement, Wrapper[].class);

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.