简体   繁体   中英

Parsing JSON in Retrofit android?

I'm trying to parse the following JSON structure using Retrofit on android.

 { "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": [] }

I have made models using http://www.jsonschema2pojo.org/ . The keys I'm particularly are payload-->name, brands-->name and subProducts-->name. Below is what I've tried so far. Can anyone please help? I can't parse this JSON Structure using retrofit

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

Interface:

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

getDService()

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

Payload.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; } }

Try using this, as you are not providing your "Payload"o bject

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

Use this code in your 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" } } }

The code will help you deserialise all the data from the JSON and you can store that wherever you want. Plus I will recommend you to keep a check on other response codes as well(such as 400 for bad request, 500 for internal server error etc). See here, you have your payload in an array, and there was only one element in it, that is why I have used payload.get(0) . In case of multiple elements in the array you need to use a loop and then fetch the values, same goes for your brands and subProduct array.

You are trying to get an object PayLoad, and you have

{ "payload": [ { "name"

this means it doesn't start with a parent, so you need to save the answer in a List like List and letter in the iteration you can use Response.get(position) <-- and this one going to be your payload number position, I hope this can help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM