我得到一些数据形式的JSON。我无法更改JSON,它是外部API。 JSON ...
"apply" : {
"type" : "submit",
"form" : "#pageForm",
"data" : {
"ctl00$MainContentArea$fld_offerCode" : "%promo"
},
"submit" : "#OfferCodeSubmit",
"response" : {
"type" : "html",
"total" : "#orderItemsDisplay .totals:last"
}
},
要么
"apply" : {
"type" : "post",
"url" : "https:\/\/www.4wheelparts.com\/shoppingcart.aspx",
"submit" : "#ctl00_ContentPlaceHolder1_btnCouponCode",
"contentType" : "application\/x-www-form-urlencoded",
"data" : {
"__VIEWSTATE" : "",
"ctl00$ContentPlaceHolder1$tbCouponCode" : "%promo",
"ctl00$ContentPlaceHolder1$btnCouponCode" : "Redeem"
}
}
我想将JSON保存到数据库,并使用“序列化数据”。但是参数“数据”不断变化。如何序列化参数“ type”,“ url”,“ submit”,而不序列化参数“ data”?
我想将此表格添加到我的数据库中...
"type" : "post"
"url" : "https:\/\/www.4wheelparts.com\/shoppingcart.aspx"
"data" : {
"__VIEWSTATE" : "",
"ctl00$ContentPlaceHolder1$tbCouponCode" : "%promo",
"ctl00$ContentPlaceHolder1$btnCouponCode" : "Redeem"
}
所以我序列化数据...
public class Apply
{
@SerializedName("type")
@Expose
private String type;
@SerializedName("submit")
@Expose
private String submit;
@SerializedName("timeout")
@Expose
private Long timeout;
....How data should look like???
还是我需要另辟move径?
最佳答案
不要在模型中添加数据,这样就可以了。我建议您使用Gson
库。并执行以下操作:
Apply apply = (new Gson()).fromJson(jsonString);
jsonString
是包含您的json的字符串变量。您可以通过将其添加到gradle文件中来导入Gson库:
compile 'com.google.code.gson:gson:2.8.0'