android – 用gson解析JSON对象

android – 用gson解析JSON对象,第1张

概述我正在尝试解析 JSON,如: {"response":[123123, 1231231, 123124, 124124, 111111, 12314]} 有了GSON,制作 Gson gson = new GsonBuilder().create();int[] friends = new Gson().fromJson(answer, int[].class);System.out.pri 我正在尝试解析 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
总结

以上是内存溢出为你收集整理的android – 用gson解析JSON对象全部内容,希望文章能够帮你解决android – 用gson解析JSON对象所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1137571.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存