Search code examples
javaandroidjsonstrapi

how can I extract data from Json which start from "["?


[
{
"id": 1,
"Name": "Banana",
"Taste": "Sweet",
"Color": "Yellow",
"Price": 4.99,
},
{
"id": 2,
"Name": "Apple",
"Taste": "Sweet",
"Color": "Red",
"Price": 5.99,
}
]

I am trying to extract data from this json which i got from strapi APIs and I am trying to do in Android

Main Activity

Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        StrapiClass strapiClass = new StrapiClass("fruits");
        String name = strapiClass.getName();
        int price = strapiClass.getPrice();
        String color = strapiClass.getColor();
        String taste = strapiClass.getTaste();
        Log.e("Strapi","Name: "+name);
        Log.e("Strapi","Price: "+price);
        Log.e("Strapi","Color: "+color);
        Log.e("Strapi","Taste: "+taste);
    }
});
thread.start();

another class

public class StrapiClass {
    final static String BASE_URL =
            "http://172.16.0.254:1337/";
    String fruits;
    String name;
    String color;
    String taste;
    int price;

public StrapiClass(String fruits) {
    this.fruits = fruits;
    getData();
}

public void getData() {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
            .url(BASE_URL + fruits)
            .get()
            .build();
    try {
        Response response = client.newCall(request).execute();
        String body = response.body().string();
        JSONArray jsonArray = new JSONArray(body);
        JSONObject jsonObject = jsonArray.getJSONObject(0);

        name = jsonObject.getString("Name");
        color = jsonObject.getString("Color");
        price = jsonObject.getInt("Price");
        taste = jsonObject.getString("Taste");
    } catch (JSONException | IOException e) {
        e.printStackTrace();
    }
}
public String getName() {
    return name;
}
public String getColor() {
    return color;
}
public String getTaste() {
    return taste;
}
public int getPrice() {
    return price;
}

after running this code I am getting this value which are default values. Please guide me how can I extract proper data in the json array.

E/Strapi: Name: null Price: 0 Color: null Taste: null


Solution

  • try to use the gson library of google

    implementation 'com.squareup.retrofit2:converter-gson:2.1.0' in your app - build.gradle - dependencies

    official link of gson: https://github.com/google/gson

    change your code:

    public class StrapiClass {
    final static String BASE_URL =
            "http://172.16.0.254:1337/";
    
    
    @SerializedName("id")
    String fruits;
    @SerializedName("Name")
    String name;
    @SerializedName("Color")
    String color;
    @SerializedName("Taste")
    String taste;
    @SerializedName("Price")
    int price;
    
    public List<StrapiClass> getData() {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(BASE_URL + fruits)
                .get()
                .build();
    
        Response response = null ;
        String body = null;
        try {
            response = client.newCall(request).execute();
            body = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Gson gson = new Gson();
        List<StrapiClass> stringList = gson.fromJson(body, new
                TypeToken<List<StrapiClass>>() {
                }.getType());
        //in stringList ,you will get everything .
        return stringList;
    }
    
    public String getName() {
        return name;
    }
    public String getColor() {
        return color;
    }
    public String getTaste() {
        return taste;
    }
    public int getPrice() {
        return price;
    }}
    

    Main Activity

    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            StrapiClass strapiClass = new StrapiClass();
            List<StrapiClass> list = strapiClass.getData();
            //you could get ererything from list
        }
    });
    thread.start()