Dedicated Server

Parse json array 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 are about to parse the below json array. Check this json array >>

Clearly, the json is of the form [{},{}..] . It is a json array, hence we can parse it as a list from android
Now inorder to generate model class/pojo class for above json, you may either create it automatically using or generate it manually

1) Automatic model class/pojo class generation using
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 in the android studio project, create a new java file and add the above generated code inside it like shown below

public class Cars {
    private String id;
    private String name;
    private String desc;

    public String getId() {
        return id;

    public void setId(String id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getDesc() {
        return desc;

    public void setDesc(String desc) {
        this.desc = desc;

Since the json is an array, we can parse it as a list like this <List<Cars>> from android. So add the below code in your file

private void parseJson() {
        Retrofit retrofit = new Retrofit.Builder()

        RequestInterface request = retrofit.create(RequestInterface.class);
        Call<List<Cars>> call1=request.getJson();
        call1.enqueue(new Callback<List<Cars>>() {
            public void onResponse(Call<List<Cars>> call, Response<List<Cars>> response) {

            public void onFailure(Call<List<Cars>> call, Throwable t) {


interface RequestInterface {
    Call<List<Cars>> getJson();

Download free source code

You may also check this tutorial to parse a json array into recyclerview

No comments:

Post a Comment