如何用android中的gson解析Json数组

如何用android中的gson解析Json数组,第1张

概述参见英文答案>HowtoParseJSONArraywithGson                                    7个我正在开发android应用程序,我正在使用gson解析json响应.我的json回应看起来像[{"customerid":"abc","customername":"abc","location":null},

参见英文答案 > How to Parse JSON Array with Gson                                    7个
我正在开发androID应用程序,我正在使用gson解析Json响应.
我的Json回应看起来像

[{"customerID":"abc","customername":"abc","location":null},{"customerID":"xyz","customername":"xyz","location":null}]

解析此响应的类看起来像:

public class CustomerInfo {       @Serializedname("customerID")    public String customerID;    @Serializedname("customername")    public String customername;    @Serializedname("location")    public String location;    public CustomerInfo()    {}}

我按照以下方式尝试了它:

  Gson gson = new Gson();  List<CustomerInfo> customers = (List<CustomerInfo>)gson.fromJson(result,CustomerInfo.class);

但它不适合我.这该怎么做.需要帮忙.谢谢.

解决方法:

首先,为CustomerInfo类中的所有变量创建getter和setter,即customerID,customername&位置,然后您可以使用下面的代码片段解析JsON:

Gson gson = new Gson();JsonData gsonData = gson.fromJson(response, CustomerInfo.class);for (int j = 0; j < gsonData.getCount(); j++) {    String customerID = gsonData.get(j).getCustomerID();    String customername = gsonData.get(j).getCustomername();    String location = gsonData.get(j).getLocation();}
总结

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

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

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

原文地址: http://www.outofmemory.cn/web/1119010.html

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

发表评论

登录后才能评论

评论列表(0条)

保存