【发布时间】:2019-10-30 06:12:21
【问题描述】:
我有一个模型,它收到一个响应,当我有 option 和 furthur 值时,在值中我有一个 priceType,它可以是百分比或固定的。 当 pricetype 是百分比时,响应是 Sting,但是当价格类型固定时,响应 id 对象并且我收到错误..
接受回复
JSONObject jsonResponse = new JSONObject(response);
Gson gson = new GsonBuilder().create();
product = gson.fromJson(String.valueOf(jsonResponse), Product.class);
百分比的 JSON 响应
{
"id": 73,
"option_id": 21,
"price": "7.0000",
"price_type": "percent",
"position": 0,
"created_at": "2019-10-22 04:23:16",
"updated_at": "2019-10-30 04:57:45",
"label": "Red",
"translations": [
{
"id": 73,
"option_value_id": 73,
"locale": "en",
"label": "Red"
}
]
}
固定价格的 Json 响应
{
"id": 74,
"option_id": 21,
"price": {
"amount": "5.0000",
"formatted": "$5.00",
"currency": "USD"
},
"price_type": "fixed",
"position": 1,
"created_at": "2019-10-22 04:23:16",
"updated_at": "2019-10-22 04:23:16",
"label": "White",
"translations": [
{
"id": 74,
"option_value_id": 74,
"locale": "en",
"label": "White"
}
]
}
价格可以是类或字符串,但我不知道如何放入我的模型类..
【问题讨论】:
标签: api android-volley