[英]JSON Parsing Android Java
decode
是一个数组而不是对象,所以应该是
JSONArray mainData = response.getJSONArray("decode");
然后,您可以使用索引获取内部对象。
JSONObject jsonObj = mainData.getJSONObject(0);
String answer = jsonObj.getString("label"); //Make
您可以尝试使用Gson
用这样的东西定义你的班级
public class YourClass implements Parcelable {
private int price;
@SerializedName("price_currency")
private String priceCurrency;
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getPriceCurrency() {
return priceCurrency;
}
public void setPriceCurrency(String priceCurrency) {
this.priceCurrency = priceCurrency;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeInt(this.price);
dest.writeString(this.priceCurrency);
}
public YourClass() {
}
protected YourClass(Parcel in) {
super(in);
this.price = in.readInt();
this.priceCurrency = in.readString();
}
public static final Creator<YourClass> CREATOR = new Creator<YourClass>() {
@Override
public Pessoa createFromParcel(Parcel source) {
return new YourClass(source);
}
@Override
public YourClass[] newArray(int size) {
return new YourClass[size];
}
};
}
并尝试用这样的东西转换你的json
Gson gson = new Gson();
YourClass yourClass = gson.fromJson(yourJson, YourClass.class);
如果您确实想正确执行此操作,则需要一个自定义JsonDeserializer
。 像这样:
public class CarDeserializer implements JsonDeserializer<Car> {
@Override
public Car deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
Car car = new Gson().fromJson(json.toString(), Car.class);
try {
JSONObject object = new JSONObject(json.toString());
car.setCurrency(Currency.getInstance(object.getString("price_currency")));
car.setBalance(object.getJSONObject("balance").getInt("API Decode"));
JSONArray decodeArray = object.getJSONArray("decode");
for (int i = 0; i < decodeArray.length(); i++){
JSONObject decodeObject = (JSONObject) decodeArray.get(i);
if (decodeObject.get("label").equals("Make")){
car.setMake(decodeObject.getString("value"));
} else if (decodeObject.get("label").equals("Manufacturer")){
car.setManufacturer(decodeObject.getString("value"));
} else if (decodeObject.get("label").equals("Plant Country")){
car.setPlantCountry(decodeObject.getString("value"));
} else if (decodeObject.get("label").equals("Product Type")){
car.setProductType(decodeObject.getString("value"));
}
}
} catch (JSONException e){
Log.e("CarDeserializer", e.toString(), e);
}
return car;
}
}
Car
对象如下所示:
public class Car {
private int price;
private transient Currency currency;
private transient int balance;
private transient String make;
private transient String manufacturer;
private transient String plantCountry;
private transient String productType;
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Currency getCurrency() {
return currency;
}
public void setCurrency(Currency currency) {
this.currency = currency;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getPlantCountry() {
return plantCountry;
}
public void setPlantCountry(String plantCountry) {
this.plantCountry = plantCountry;
}
public String getProductType() {
return productType;
}
public void setProductType(String productType) {
this.productType = productType;
}
}
如果Currency
不适用于您,则可以将其更改为类似于以下的String
类型:
@SerializedName("price_currency") private String currency;
并相应地更改getter
和setter
。
如果decode
数组中有更多对象。 您可以将它们添加为反序列化器中的更多分支。
这样使用:
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Car.class, new CarDeserializer());
Gson gson = gsonBuilder.create();
gson.fromJson(myJsonString, Car.class);
注意: Car
类中的transient
关键字向Gson
指示,它不应尝试自动解析这些字段的json。
注意2:如果尚未添加Gson,则需要在项目中包括它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.