Tipok
Tipok

Reputation: 695

Serialize only the data you need from json

I get some data form JSON. I can't change JSON, it is foreign API. JSON...

"apply" : {
              "type"   : "submit",
              "form"   : "#pageForm",
              "data"   : {
                             "ctl00$MainContentArea$fld_offerCode" : "%promo"
                         },
              "submit"   : "#OfferCodeSubmit",
              "response" : {
                               "type"  : "html",
                               "total" : "#orderItemsDisplay .totals:last"
                           }
          },

or

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

I want save JSON to database, and use "serialize data". But parameter "data" constantly changing. How can I serialize parameter "type", "url", "submit", and is't serialize parametr "data"?

I want to add to my DB this form...

"type" : "post"
"url"  : "https:\/\/www.4wheelparts.com\/shoppingcart.aspx"
"data" : {
             "__VIEWSTATE"                             : "",
             "ctl00$ContentPlaceHolder1$tbCouponCode"  : "%promo",
             "ctl00$ContentPlaceHolder1$btnCouponCode" : "Redeem"
         }

So I serialize the data...

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

Or am I going to need to move the another way?

Upvotes: 1

Views: 180

Answers (4)

Tipok
Tipok

Reputation: 695

I solved it problem with help:

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

I get json without serialization, and than parse json

//item - (String) content of json file.

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

And now required is equal to String : {"type":"submit","form":"#pageForm","data":{"ctl00$MainContentArea$fld_offerCode":"%promo"}},

And i just save in DB this string, this is what I needed. Thanks all.

Upvotes: 0

Muhammad Saad Rafique
Muhammad Saad Rafique

Reputation: 3189

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);
}

}

Upvotes: 1

Hema
Hema

Reputation: 986

If you need only few specific data,Can follow this

jsonObj = new JSONObject(strRequestPacket);
requiredData = jsonObj.getString("requiredData");

If you need to map to your entity class,then follow this

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

Upvotes: 1

Amir Ziarati
Amir Ziarati

Reputation: 15097

dont add data in your model and that will be ok. i suggest you to use Gson library. and do as below :

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

jsonString is a string variable containing your json.

you can import Gson library by adding this to your gradle file:

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

Upvotes: 1

Related Questions