Android JSON 中的排球阵列
Android Volley Array in JSON
我有这种JSON反应
{"error":false,"country":"United Kingdom","country_id":"903",
"currency":"GBP","product_list":["5","10","15","20","25","30","40","50"]}
而且我能够毫无问题地解析国家/地区、country_id 和货币,当我尝试解析产品列表时出现问题!代码下方
try {
boolean error = response.getBoolean("error");
if (!error){
String country = response.getString("country");
int country_id = response.getInt("country_id");
String currency = response.getString("currency");
List<Tarif> tarifs = new
Gson().fromJson(response.getJSONArray("product_list").toString(), new
TypeToken<List<Tarif>>(){}.getType());
new DtoneTarifs(country, country_id, currency, tarifs);
}
}
这是我的关税和其他 Class
public class Tarifs {
public String country;
public int country_id;
public String currency;
public List<Tarif> tarifList;
public Tarifs (String country, int country_id, String currency, List<Tarif> tarif){
this.country = country;
this.country_id = country_id;
this.currency = currency;
this.tarifList = tarif;
}
}
我想在 Tarif class 中填写 product_list,其中只有一个参数接受并在 recycler_view 中显示
{"error":false,"country":"United Kingdom","country_id":"903",
"currency":"GBP","product_list":["5","10","15","20","25","30","40","50"]}
您可以看到 product_list 是 JSON 字符串值数组。但是您正在将其转换为 Tarif 类型的列表。应该转换成字符串类型的列表。
将作为自定义对象的 Tarif 的值设置为 JSON 数组或将您的列表类型更改为字符串。
应该是这样的:
try {
boolean error = response.getBoolean("error");
if (!error){
String country = response.getString("country");
int country_id = response.getInt("country_id");
String currency = response.getString("currency");
List<String> tarifs = new
Gson().fromJson(response.getJSONArray("product_list").toString(), new
TypeToken<List<String>>(){}.getType());
Tarifs result = new Tarifs(country, country_id, currency, tarifs);
}
}
关税 Class
public class Tarifs {
public String country;
public int country_id;
public String currency;
public List<String> tarifList;
public Tarifs (String country, int country_id, String currency, List<String> tarif){
this.country = country;
this.country_id = country_id;
this.currency = currency;
this.tarifList = tarif;
}
}
给你!
我有这种JSON反应
{"error":false,"country":"United Kingdom","country_id":"903",
"currency":"GBP","product_list":["5","10","15","20","25","30","40","50"]}
而且我能够毫无问题地解析国家/地区、country_id 和货币,当我尝试解析产品列表时出现问题!代码下方
try {
boolean error = response.getBoolean("error");
if (!error){
String country = response.getString("country");
int country_id = response.getInt("country_id");
String currency = response.getString("currency");
List<Tarif> tarifs = new
Gson().fromJson(response.getJSONArray("product_list").toString(), new
TypeToken<List<Tarif>>(){}.getType());
new DtoneTarifs(country, country_id, currency, tarifs);
}
}
这是我的关税和其他 Class
public class Tarifs {
public String country;
public int country_id;
public String currency;
public List<Tarif> tarifList;
public Tarifs (String country, int country_id, String currency, List<Tarif> tarif){
this.country = country;
this.country_id = country_id;
this.currency = currency;
this.tarifList = tarif;
}
}
我想在 Tarif class 中填写 product_list,其中只有一个参数接受并在 recycler_view 中显示
{"error":false,"country":"United Kingdom","country_id":"903",
"currency":"GBP","product_list":["5","10","15","20","25","30","40","50"]}
您可以看到 product_list 是 JSON 字符串值数组。但是您正在将其转换为 Tarif 类型的列表。应该转换成字符串类型的列表。
将作为自定义对象的 Tarif 的值设置为 JSON 数组或将您的列表类型更改为字符串。
应该是这样的:
try {
boolean error = response.getBoolean("error");
if (!error){
String country = response.getString("country");
int country_id = response.getInt("country_id");
String currency = response.getString("currency");
List<String> tarifs = new
Gson().fromJson(response.getJSONArray("product_list").toString(), new
TypeToken<List<String>>(){}.getType());
Tarifs result = new Tarifs(country, country_id, currency, tarifs);
}
}
关税 Class
public class Tarifs {
public String country;
public int country_id;
public String currency;
public List<String> tarifList;
public Tarifs (String country, int country_id, String currency, List<String> tarif){
this.country = country;
this.country_id = country_id;
this.currency = currency;
this.tarifList = tarif;
}
}
给你!