我想解析这个json,但我做不到。以下是Json结构:

例如:我想要字符串产品类型的=>轿车
本守则不起作用:
JSONObject mainData = response.getJSONObject("decode");
String productType = mainData.getString("Product Type");请帮帮忙
发布于 2018-06-11 16:49:30
decode是一个数组,而不是一个对象,所以它应该是
JSONArray mainData = response.getJSONArray("decode");然后可以使用索引获取内部对象。
JSONObject jsonObj = mainData.getJSONObject(0);
String answer = jsonObj.getString("label"); //Make发布于 2018-06-11 17:05:31
你可以试着用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);发布于 2018-06-11 17:25:23
如果您真的想正确地这样做,您将需要一个自定义的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);注意:transient类中的Car关键字向Gson表明它不应该尝试自动解析这些字段的json。
注2:如果尚未添加Gson,则需要在项目中包括Gson。
https://stackoverflow.com/questions/50802472
复制相似问题