Reputation: 775
I'm trying to parse JSON like:
{"response":[123123, 1231231, 123124, 124124, 111111, 12314]}
With GSON, making
Gson gson = new GsonBuilder().create();
int[] friends = new Gson().fromJson(answer, int[].class);
System.out.print(friends[0]);
But get Error Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
How to parse this numbers in array?
Upvotes: 8
Views: 31432
Reputation: 2308
A Kotlin soulution
val myResponse = Gson().fromJson(responseStr, MyResponse::class.java)
Upvotes: 1
Reputation: 76918
The other option you have outside of using a wrapper class is simply to get the array from the parse tree.
Use the JsonParser
to create the tree, get your array from it, then convert to int[]
using Gson
:
public class App
{
public static void main( String[] args ) throws IOException
{
String json = "{\"response\":[1,2,3,4,5]}";
JsonObject jo = new JsonParser().parse(json).getAsJsonObject();
JsonArray jsonArray = jo.getAsJsonArray("response");
int[] myArray = new Gson().fromJson(jsonArray, int[].class);
System.out.println(Arrays.toString(myArray));
}
}
Note that you could also just work with the JsonArray
directly and not convert it to an int[]
depending on your use case.
System.out.println(jsonArray.get(0).getAsInt());
Upvotes: 8
Reputation: 15543
Json value has an object as response which holds an integer array. You have to define its class correctly first:
class ResponseHolder {
private Integer[] response;
}
or
class ResponseHolder {
private int[] response;
}
or
class ResponseHolder {
private String[] response;
}
Then use that class while parsing your json value to a java instance:
ResponseHolder responseHolder = gson.fromJson(answer, ResponseHolder.class);
Upvotes: 0
Reputation: 7693
Try this method
String json1 = "[{\"contactName\":\"3\",\"contactNumber\":\"3\"},{\"contactName\":\"4\",\"contactNumber\":\"4\"}]";
JsonElement json = new JsonParser().parse(json1);
JsonArray array= json.getAsJsonArray();
Iterator iterator = array.iterator();
List<ContactDetail> details = new ArrayList<ContactDetail>();
while(iterator.hasNext()){
JsonElement json2 = (JsonElement)iterator.next();
Gson gson = new Gson();
ContactDetail contact = gson.fromJson(json2, ContactDetail.class);
//can set some values in contact, if required
details.add(contact);
}
I got another form here
Upvotes: 1
Reputation: 3994
BEGIN_ARRAY
but was BEGIN_OBJECT
means in the beginning it's Object not the array. If you will see in the Json, response property is the type of object and that object further contains the array. You can Parse it with different ways Using creating your own custom model class or without creating custom model class. If you don't wan't to create your Response Model class then simply parse it
final Type typeOf = new TypeToken<Map<String, List<Integer>>>() {}.getType();
final Map<String, List<Integer>> map = new Gson().fromJson(Your_JSON_String, typeOf);
// finally get list of ints form map.
final List<Integer> listOfIntegers =map.get("response");
Upvotes: 0
Reputation: 547
You're going to want to create a model class first that GSON can bind your json to:
public class ResponseModel {
private List<Integer> response = new ArrayList<Integer>();
public List<Integer> getResponse() {
return response;
}
@Override
public String toString() {
return "ResponseModel [response=" + response + "]";
}
}
Then you can call
Gson gson = new Gson();
ResponseModel responseModel = gson.fromJson("{\"response\":[123123, 1231231, 123124, 124124, 111111, 12314]}",
ResponseModel.class);
List <Integer> responses = responseModel.getResponse();
// ... do something with the int list
Upvotes: 14
Reputation: 2903
Or use org.json included in Android:
JSONObject jsonData = new JSONObject(answer);
JSONArray jsonItems = jsonData.getJSONArray("response");
Upvotes: -2