0

I have this kind of JSON response

{"error":false,"country":"United Kingdom","country_id":"903",
"currency":"GBP","product_list":["5","10","15","20","25","30","40","50"]}

And I am able to parse country, country_id, and currency without a problem, problem starts with the product list when I am trying to parse it! below the code

 try {
 boolean error = response.getBoolean("error");
 if (!error){ 
 String country = response.getString("country");
 int country_id = response.getInt("country_id");
 String currency = response.getString("currency");
 List<Tarif> tarifs = new 
 Gson().fromJson(response.getJSONArray("product_list").toString(), new 
 TypeToken<List<Tarif>>(){}.getType());
 new DtoneTarifs(country, country_id, currency, tarifs);
 }
 }

And here is my Tarif and Other Class

public class Tarifs {
public String country;
public int country_id;
public String currency;
public List<Tarif> tarifList;
public Tarifs (String country, int country_id, String currency, List<Tarif> tarif){
 this.country = country;
 this.country_id = country_id;
 this.currency = currency;
 this.tarifList = tarif;
}
}

I want to fill the product_list in Tarif class where only one parameter accept and show them in recycler_view

asked Mar 19, 2020 at 17:58

1 Answer 1

1
{"error":false,"country":"United Kingdom","country_id":"903",
"currency":"GBP","product_list":["5","10","15","20","25","30","40","50"]}

You can see that product_list is JSON Array of string values. But you are converting it into list of Tarif type. It should be converted into list of string type.

Either set values of Tarif as custom objects to JSON Array or change your list type to string.

It should be like this:

try {
 boolean error = response.getBoolean("error");
 if (!error){ 
 String country = response.getString("country");
 int country_id = response.getInt("country_id");
 String currency = response.getString("currency");
 List<String> tarifs = new 
 Gson().fromJson(response.getJSONArray("product_list").toString(), new 
 TypeToken<List<String>>(){}.getType());
 Tarifs result = new Tarifs(country, country_id, currency, tarifs);
 }
 }

Tarifs Class

public class Tarifs {
public String country;
public int country_id;
public String currency;
public List<String> tarifList;
public Tarifs (String country, int country_id, String currency, List<String> tarif){
 this.country = country;
 this.country_id = country_id;
 this.currency = currency;
 this.tarifList = tarif;
}
}

Here you go!

answered Mar 19, 2020 at 18:22
Sign up to request clarification or add additional context in comments.

6 Comments

and then how can I retrieve their values and fetch them in recycler view?
Please following this link to learn how to show list of items in recyclerview. codebrainer.com/blog/how-to-display-data-with-recyclerview
thank you let me try, by the way how can I pass ArrayList with the intent
Intent i=new Intent(this,YourActivity.class); i.putStringArrayListExtra("list", tarifs);
Thanks let me try, I will inform you if everything is alright
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.