2017-04-13 2 views
1

Ich bekomme einige Daten von JSON. Ich kann JSON nicht ändern, es ist eine fremde API. JSON ...Serialisieren Sie nur die Daten, die Sie von JSON benötigen

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

oder

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

ich Datenbank JSON möchten speichern und verwenden "Daten serialisieren". Aber der Parameter "Daten"ändert sich ständig. Wie kann ich serialisiert Parameter "type", "url", "Eintragen" und is't parametr "data" serialisiert?

ich meine DB diese Form ...

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

So serialisieren ich die Daten ...

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

Oder werde ich den anderen Weg zu bewegen, brauchen?

Antwort

0

Ich löste es Problem mit Hilfe:

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

ich json ohne Serialisierung zu bekommen, und als Parse-json

// item - (String) Inhalt der JSON-Datei.

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

Und jetzt erforderlich ist gleich String: {"type":"submit","form":"#pageForm","data":{"ctl00$MainContentArea$fld_offerCode":"%promo"}},

und ich spare nur diese Zeichenfolge in DB, das ist, was ich brauchte. Danke allen.

1

fügen Sie keine Daten in Ihr Modell und das wird in Ordnung sein. Ich schlage vor, Sie verwenden Bibliothek. und tut, wie unten:

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

jsonString ist ein String-Variable mit Ihrem json.

können Sie Gson Bibliothek importieren, indem Sie diese auf Ihre gradle Datei hinzufügen:

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

kann ich nicht finden - wie kann ich JSON im Format String mit Hilfe nachrüsten \ rxjava bekommen. Bist du das? –

1

Wenn Sie nur wenige spezifische Daten benötigen, folgen Sie diesem

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

Wenn Sie Ihr Unternehmen zuordnen müssen Klasse, dann folgen Sie diesem

Gson gson = new Gson(); 
if(mob_farmerStatus !=null){ 
User user = gson.fromJson(strRequestPacket, User.class); 
System.out.println("user:::"+user); 
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); 
} 

} 
+0

"Daten" ständig wechselnden Freund. Wenn ich das tue. Jedes Mal, wenn ich json ändere, muss ich die Code-Anwendung ändern. :( –

Verwandte Themen