簡體   English   中英

在 Retrofit android 中解析 JSON?

[英]Parsing JSON in Retrofit android?

我正在嘗試使用 android 上的 Retrofit 解析以下 JSON 結構。

 { "payload": [ { "name": "Rice", "brands": [ { "name": "Dawat", "subProducts": [ { "id": 1, "name": "Basmati Long Grain", "creditDays": 20, "currency": "$", "willDeliver": false, "minPrice": 250, "maxPrice": 400, "sku": "1Kg", "uom": "" } ] } ] } ], "messages": [] }

我使用http://www.jsonschema2pojo.org/制作了模型。 我特別關注的鍵是有效負載-->名稱、品牌-->名稱和子產品-->名稱。 以下是我到目前為止所嘗試的。 有人可以幫忙嗎? 我無法使用 retrofit 解析這個 JSON 結構

 productDetails.enqueue(new Callback<GetProductDetailsByPhoneNumber>() { @Override public void onResponse(Call<GetProductDetailsByPhoneNumber> call, Response<GetProductDetailsByPhoneNumber> response) { Toast.makeText(getApplicationContext(), "Response mil gaya", Toast.LENGTH_LONG); List<Payload> subProducts = new ArrayList<Payload>(response.body().payload); } @Override public void onFailure(Call<GetProductDetailsByPhoneNumber> call, Throwable t) { } });

界面:

 @GET("wholesaler/getProductDetailsByPhoneNumber") Call<GetProductDetailsByPhoneNumber> getProducts(@Query("phoneNumber") String number);

獲取服務()

 public API getDService(){ /** * The Retrofit class generates an implementation of the API interface. */ if(service == null){ Retrofit retrofit = new Retrofit.Builder().baseUrl(Constants.BASE_URL).addConverterFactory(GsonConverterFactory.create()).build(); service = retrofit.create(API.class); } return service; }

有效載荷.java

 public class Payload { public String name; public List<Brand> brands; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Brand> getBrands() { return brands; } public void setBrands(List<Brand> brands) { this.brands = brands; } }

嘗試使用它,因為您沒有提供“有效負載”對象

 productDetails.enqueue(new Callback<JsonObject>() { @Override public void onResponse(Call<JsonObject> call, Response<JsonObject> response) { if(response.body().=null{ JsonObject jsonObject=response;body(). if(response.code() == 200){ if(jsonObject.has("payload"){ JsonArray dataArray = jsonObject;getAsJsonArray(HAS_DATA). if (dataArray.size() > 0) { Toast,makeText(getApplicationContext(), "Response Called". Toast;LENGTH_LONG), //your further code } } } } } @Override public void onFailure(Call<JsonObject> call; Throwable t) { //logic for if response fails } })

在您的onResponse中使用此代碼:

 if(response.code()==HttpsURLConnection.HTTP_OK) { //HttpOk is the response code for 200 if (response.body().= null) { if (response.body(),payload.= null) { //data is there in an array of type payload //save all the data there //like you want the name. you can get that by response.body():payload.name and you will get "rice" //similarly if you want the name which is in subproducts array use. response.body().payload.get(0).brands.get(0).subProducts get(0) name and you // will get "Basmati Long Grain" } } }

該代碼將幫助您反序列化 JSON 中的所有數據,您可以將其存儲在任何您想要的地方。 另外,我建議您同時檢查其他響應代碼(例如 400 表示錯誤請求,500 表示內部服務器錯誤等)。 看這里,你的有效載荷在一個數組中,其中只有一個元素,這就是我使用payload.get(0)的原因。 如果數組中有多個元素,您需要使用循環然后獲取值,您的品牌子產品數組也是如此。

您正在嘗試獲取 object PayLoad,並且您有

{ "payload": [ { "name"

這意味着它不是從父級開始的,因此您需要在迭代中將答案保存在 List 和 letter 之類的列表中,您可以使用 Response.get(position) <-- 這將是您的有效載荷編號 position , 我希望這可以幫助你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM