如何在android中用gson解析Json数组
我正在开发android应用程序,并使用gson解析json响应。 我的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")
[{"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解析到的类