How to Parse JSON Parsing Using Gson in Android

How to parse JSON using GSON in android

You are trying to parse an array of object (from your webservices) to an object containing a property mResults which is an array of object (Test);

Instead of using Results class during parsing you need to directly use an ArrayList of Test:

Results results = gson.fromJson(response.toString(), Results.class);

should be

ArrayList<Test> results = gson.fromJson(response.toString(), new TypeToken<ArrayList<Test>>(){}.getType());

As ArrayList is a generic it's not possible to use ArrayList<Test>.class to get a representation of the data type, we must use TypeToken. What is TypeToken?

TypeToken represents a generic type T. Java doesn't yet provide a way to
represent generic types, so this class does. Forces clients to create
a subclass of this class which enables retrieval the type information
even at runtime.

getType returns the type of the class used to build TypeToken and so we can use it in gson fromJson() parsing method.

--

Second problem:

Your are using JsonObjectRequest but server response is a Json array so you need to use JsonArrayRequest and so update MyListner to use JSONArray instead of JSONObject.

BUT

As your are parsing server response manually you can use StringRequest and so avoid parsing step of Volley.

StringRequest jsonObjectRequest = new StringRequest(
Request.Method.GET,
"https://api.github.com/repos/crashlytics/secureudid/issues",
new MyListner(),
new MyErrorListner()
);

and MyListner is now directly using String:

class MyListner implements Response.Listener<String> {
@Override
public void onResponse(String response) {
Gson gson = new Gson();
ArrayList<Test> results = gson.fromJson(response, new TypeToken<ArrayList<Test>>() {}.getType());

for (Test t : results) {
Log.e("Tag", t.toString());
}
}
}

Using Gson in Kotlin to parse JSON array

You need to change parameter in your fromJson() function call like following:

val weatherList: List<WeatherObject> = gson.fromJson(stringReader , Array<WeatherObject>::class.java).toList()

You need to pass Array<WeatherObject>::class.java for class type and then convert result into List. No need to change registerTypeAdapter() function call.

Check following code:

fun getWeatherObjectFromJson(jsonStr: String): List<WeatherObject> {

var stringReader: StringReader = StringReader(jsonStr)
var jsonReader: JsonReader = JsonReader(stringReader)

val gsonBuilder = GsonBuilder().serializeNulls()
gsonBuilder.registerTypeAdapter(WeatherObject::class.java, WeatherDeserializer())
val gson = gsonBuilder.create()

val weatherList: List<WeatherObject> = gson.fromJson(stringReader , Array<WeatherObject>::class.java).toList()

return weatherList
}

How to use gson and volley parse nested json?

1- You have to update your bean class as follows :-

public class People implements Serializable {
private String name ;
private String email;
private Phone phone;

public Phone getPhone () {
return phone;
}

public void setPhone (Phone phone) {
this.phone = phone;
}
public String getName () {
return name;
}

public void setName (String name) {
this.name = name;
}

public String getEmail () {
return email;
}

public void setEmail (String email) {
this.email = email;
}}

2- Create a new bean class Phone.java :-

public class Phone implements Serializable{
private String home;

public String getMobile () {
return mobile;
}

public void setMobile (String mobile) {
this.mobile = mobile;
}

public String getHome () {
return home;
}

public void setHome (String home) {
this.home = home;
}

private String mobile;}

3- Now update your code as follows:-

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, APITEST,null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject jsonObject) {

Gson gson = new Gson();
People people;
people = gson.fromJson(jsonObject.toString(),People.class);
tv_city.setText(""+people.email);
//for getting Home & Mobile number
String home=people.getPhone.getHome();
String mobile=people.getPhone.getMobile();

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {



}
});

Note:- My above bean is as per expected api response in your question. But if you have nested objects then you have to choose either List<Phone> or ArrayList<Phone> inside in your People bean and then create its getters and setters.

Hope this will help you !!!



Related Topics



Leave a reply



Submit