Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中用gson解析Json数组_Android_Json_Gson - Fatal编程技术网

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

如何在android中用gson解析Json数组,android,json,gson,Android,Json,Gson,我正在开发android应用程序,并使用gson解析json响应。 我的json响应看起来像 [{"customerid":"abc","customername":"abc","location":null} ,{"customerid":"xyz","customername":"xyz","location":null}] 用于分析此响应的类如下所示: public class CustomerInfo { @SerializedName("customerid")

我正在开发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);
Gson-Gson=new-Gson();
List customers=(List)gson.fromJson(结果,CustomerInfo.class);

但这对我不起作用。如何做到这一点。我需要帮助。谢谢。

首先为CustomerInfo类中的所有变量创建getter和setter,即customerid、customername和location,然后可以使用以下代码段解析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();
}
Gson-Gson=new-Gson();

JsonData gsonData=gson.fromJson(响应,CustomerInfo.class); 对于(int j=0;j
JsonData gsonData=gson.fromJson??!!!JsonData gsonData=gson.fromJson(响应,CustomerInfo.class);//响应是字符串形式的,CustomerInfo.class是json解析到的类