仅从json序列化您需要的数据

时间:2017-04-13 04:11:30

标签: java android json retrofit

我从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”“提交”,并且不能序列化参数“数据”

我想将此表单添加到我的数据库中......

"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???

或者我是否需要换另一种方式?

4 个答案:

答案 0 :(得分:1)

不要在模型中添加数据,这样就可以了。我建议你使用Gson库。并按以下步骤操作:

Apply apply = (new Gson()).fromJson(jsonString);

jsonString是一个包含json的字符串变量。

您可以通过将Gson库添加到您的gradle文件来导入Gson库:

  compile 'com.google.code.gson:gson:2.8.0'

答案 1 :(得分:1)

如果您只需要少量特定数据,可以按照

进行操作
jsonObj = new JSONObject(strRequestPacket);
requiredData = jsonObj.getString("requiredData");

如果您需要映射到您的实体类,请按照

进行操作
Gson gson = new Gson();
if(mob_farmerStatus !=null){
User user = gson.fromJson(strRequestPacket, User.class);
System.out.println("user:::"+user);

答案 2 :(得分:1)

public class Apply
{
    @SerializedName("type")
    @Expose
    private String type;
    @SerializedName("submit")
    @Expose
    private String submit;
    @SerializedName("timeout")
    @Expose
    private Long timeout;
    @SerializedName("timeout")
    @Expose
    private Data data;

// Setter Getters here
}

public class Data 
{

private String vIEWSTATE;
private String ctl00$ContentPlaceHolder1$tbCouponCode;
private String ctl00$ContentPlaceHolder1$btnCouponCode;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public String getVIEWSTATE() {
return vIEWSTATE;
}

public void setVIEWSTATE(String vIEWSTATE) {
this.vIEWSTATE = vIEWSTATE;
}

public String getCtl00$ContentPlaceHolder1$tbCouponCode() {
return ctl00$ContentPlaceHolder1$tbCouponCode;
}

public void setCtl00$ContentPlaceHolder1$tbCouponCode(String ctl00$ContentPlaceHolder1$tbCouponCode) {
this.ctl00$ContentPlaceHolder1$tbCouponCode = ctl00$ContentPlaceHolder1$tbCouponCode;
}

public String getCtl00$ContentPlaceHolder1$btnCouponCode() {
return ctl00$ContentPlaceHolder1$btnCouponCode;
}

public void setCtl00$ContentPlaceHolder1$btnCouponCode(String ctl00$ContentPlaceHolder1$btnCouponCode) {
this.ctl00$ContentPlaceHolder1$btnCouponCode = ctl00$ContentPlaceHolder1$btnCouponCode;
}

public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

答案 3 :(得分:0)

我用帮助解决了这个问题:

import okhttp3.ResponseBody;
public interface ConfigsBodyRequest
{
    @GET("config.json")
    Observable<ResponseBody> getResponse();
}

我没有序列化得到json,而是解析json

//项目 - (字符串)json文件的内容。

 JSONObject jsonObject = new JSONObject(item);
 String required = jsonObject.getString("apply");

现在需要等于String:{"type":"submit","form":"#pageForm","data":{"ctl00$MainContentArea$fld_offerCode":"%promo"}},

我只是在DB中保存这个字符串,这就是我需要的。谢谢大家。