我正在尝试解析
JSON,如:
{"response":[123123,1231231,123124,124124,111111,12314]}
有了GSON,制作
Gson gson = new GsonBuilder().create(); int[] friends = new Gson().fromJson(answer,int[].class); System.out.print(friends[0]);
但是获得错误预期BEGIN_ARRAY但在第1行第2列是BEGIN_OBJECT
如何在数组中解析这些数字?
解决方法
您将首先想要创建一个模型类,GSON可以将您的json绑定到:
public class ResponseModel { private List<Integer> response = new ArrayList<Integer>(); public List<Integer> getResponse() { return response; } @Override public String toString() { return "ResponseModel [response=" + response + "]"; } }
然后你可以打电话
Gson gson = new Gson(); ResponseModel responseModel = gson.fromJson("{\"response\":[123123,12314]}",ResponseModel.class); List <Integer> responses = responseModel.getResponse(); // ... do something with the int list