【发布时间】:2017-07-23 05:07:12
【问题描述】:
我的 JSON 字符串。
{
"RateCardType": [{
"rate_id": 32,
"applianceId": 59,
"categoryId": 33,
"install_Price": 599,
"uninstall_Price": 0,
"gasRefill_Price": 0,
"repair_Price": 249,
"basicClean_Price": 0,
"deepClean_Price": 449,
"demo_Price": 500
},
{
"rate_id": 33,
"applianceId": 59,
"categoryId": 34,
"install_Price": 799,
"uninstall_Price": 0,
"gasRefill_Price": 0,
"repair_Price": 349,
"basicClean_Price": 0,
"deepClean_Price": 799,
"demo_Price": 500
}
]
}
MyRateCard.java
package com.example.demo;
import javax.persistence.Column;
public class MyRateCard {
@Column(name = "rate_id")
int rate_id;
public void setRate_id(int rate_id) {
this.rate_id=rate_id;
}
public int getRate_id() {
return rate_id;
}
@Column(name = "applianceId")
int applianceId;
public void setApplianceId(int applianceId {
this.applianceId=applianceId;
}
public int getApplianceId() {
return applianceId;
}
@Column(name = "categoryId")
int categoryId;
public void setCategoryId(int categoryId) {
this.categoryId=categoryId;
}
public int getCategoryId() {
return categoryId;
}
@Column(name = "install_Price")
int install_Price;
public void setInstall_Price(int install_Price {
this.install_Price=install_Price;
}
public int getInstall_Price() {
return install_Price;
}
@Column(name = "uninstall_Price")
int uninstall_Price;
public void setUninstall_Price(int uninstall_Price) {
this.uninstall_Price=uninstall_Price;
}
public int getUninstall_Price() {
return uninstall_Price;
}
@Column(name = "gasRefill_Price")
int gasRefill_Price;
public void setGasRefill_Price(int gasRefill_Price) {
this.gasRefill_Price=gasRefill_Price;
}
public int getGasRefill_Price() {
return gasRefill_Price;
}
@Column(name = "repair_Price")
int repair_Price;
public void setRepair_Price(int repair_Price) {
this.repair_Price=repair_Price;
}
public int getRepair_Price() {
return repair_Price;
}
@Column(name = "basicClean_Price")
int basicClean_Price;
public void setBasicClean_Price(int basicClean_Price) {
this.basicClean_Price=basicClean_Price;
}
public int getBasicClean_Price() {
return basicClean_Price;
}
@Column(name = "deepClean_Price")
int deepClean_Price;
public void setDeepClean_Price(int deepClean_price) {
this.deepClean_Price=deepClean_price;
}
public int getDeepClean_Price() {
return deepClean_Price;
}
@Column(name = "demo_Price")
int demo_Price;
public void setDemo_Price(int demo_Price) {
this.demo_Price=demo_Price;
}
public int getDemo_Price() {
return demo_Price;
}
}
我创建了一个包含所有 getter 和 setter 的模型类 MyRateCard.java。我想用 JSON 字符串中的第一个对象为 MyRateCard 创建一个对象(比如 rate_id:32)。
MyRateCard ratecard = new Gson().fromJson(response.toString(), MyRateCard.class);
但不工作。有人可以帮我解析一下吗?
【问题讨论】:
-
附上
MyRateCard类的代码。 -
this 有帮助吗?
-
MyRateCard 类列表中你使用过任何类吗?
-
我是这个概念的新手,所以不知道该怎么做。 @AvijitKarmakar 我没有使用任何东西。