Showing posts with label json to java object online. Show all posts
Showing posts with label json to java object online. Show all posts
Android Recyclerview with Cardview example : json parsing using retrofit
Parse a json array inside another json object in android with free source code download
We will be using retrofit,gson for parsing, so go to your app level build.gradle and add the following as dependencies
implementation 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'please add the latest dependency versions for retrofit, converter-gson
Hi, in this tutorial, we will parse the below json. Check this json here >> https://navneet7k.github.io/sample_object_array.json
![]() |
Here you can see that the cars_array is wrapped inside another json object, so we will need two model/pojo classes |
1) Automatic model class/pojo class generation using http://www.jsonschema2pojo.org/
You can make use of jsonschema2pojo to covert json to java object online. This is a really effective tool which even supports various annotation types like jackson, gson, moshi etc.
- So, first of all, go to http://www.jsonschema2pojo.org/
- Paste the json https://navneet7k.github.io/sample_object_array.json in the box provided
![]() |
Specify Target language as Java, Source Type as JSON and annotation style as Gson |
- Now click preview and copy the generated java files
![]() |
Now copy the contents of the class and add it in android studio project |
SampleResponse.java
public class SampleResponse { @SerializedName("id") @Expose private String id; @SerializedName("type") @Expose private String type; @SerializedName("cars_array") @Expose private List<CarsArray> carsArray = null; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getType() { return type; } public void setType(String type) { this.type = type; } public List<CarsArray> getCarsArray() { return carsArray; } public void setCarsArray(List<CarsArray> carsArray) { this.carsArray = carsArray; } }
CarsArray.java
public class CarsArray { @SerializedName("id") @Expose private String id; @SerializedName("name") @Expose private String name; @SerializedName("desc") @Expose private String desc; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } }
MainActivity.java
private void parseJson() { Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://navneet7k.github.io/") .addConverterFactory(GsonConverterFactory.create()) .build(); RequestInterface request = retrofit.create(RequestInterface.class); Call<SampleResponse> call1=request.getJson(); call1.enqueue(new Callback<SampleResponse>() { @Override public void onResponse(Call<SampleResponse> call, Response<SampleResponse> response) { Toast.makeText(MainActivity.this,"Success! response for first item >> \n car :" +response.body().getCarsArray().get(0).getName()+"\ndesc :"+response.body().getCarsArray().get(0).getDesc(),Toast.LENGTH_SHORT).show(); } @Override public void onFailure(Call<SampleResponse> call, Throwable t) { Toast.makeText(MainActivity.this,"Failure",Toast.LENGTH_SHORT).show(); } }); }
RequestInterface.java
interface RequestInterface { @GET("sample_object_array.json") Call<SampleResponse> getJson(); }
Download free source code
Parse json object in android with free source code download
We will be using retrofit,gson for parsing, so go to your app level build.gradle and add the following as dependencies
implementation 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'please add the latest dependency versions for retrofit, converter-gson
Hi, we will be parsing the below json in this tutorial. Check this json here >> https://navneet7k.github.io/sample_object.json
![]() |
Clearly, the json is of the form {..} and we can parse it as an object from android |
1) Automatic model class/pojo class generation using http://www.jsonschema2pojo.org/
You can make use of jsonschema2pojo to covert json to java object online. This is a really effective tool which even supports various annotation types like jackson, gson, moshi etc.
- So, first of all, go to http://www.jsonschema2pojo.org/
- Paste the json https://navneet7k.github.io/sample_object.json in the box provided
![]() |
Specify Target language as Java, Source Type as JSON and annotation style as Gson |
- Now click preview and copy the generated java files
![]() |
Now copy the contents of the class and add it in android studio project |
Now copy the above generated contents into SampleJson.java as shown below
SampleJson.java
public class SampleJson { @SerializedName("title") @Expose private String title; @SerializedName("description") @Expose private String description; @SerializedName("name") @Expose private String name; @SerializedName("id") @Expose private String id; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } }
MainActivity.java
private void parseJson() { Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://navneet7k.github.io/") .addConverterFactory(GsonConverterFactory.create()) .build(); RequestInterface request = retrofit.create(RequestInterface.class); Call<SampleJson> call1=request.getJson(); call1.enqueue(new Callback<SampleJson>() { @Override public void onResponse(Call<SampleJson> call, Response<SampleJson> response) { Toast.makeText(MainActivity.this,"Success!",Toast.LENGTH_SHORT).show(); } @Override public void onFailure(Call<SampleJson> call, Throwable t) { Toast.makeText(MainActivity.this,"Failure",Toast.LENGTH_SHORT).show(); } }); }
RequestInterface.java
interface RequestInterface { @GET("sample_object.json") Call<SampleJson> getJson(); }
Download free source code
Subscribe to:
Posts (Atom)