简体   繁体   中英

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

you can use lib retrofit from https://square.github.io/retrofit/ load data from the server

and create Object:

public class StrapiClass {
    @SerializedName("Name")
    String Name;
    @SerializedName("Color")
    String Color;
    @SerializedName("Price")
    String Price;
    @SerializedName("Taste")
    String Taste;

    public StrapiClass() {
    }

    public String getName() {
        return Name;
    }

    public String getColor() {
        return Color;
    }

    public String getPrice() {
        return Price;
    }

    public String getTaste() {
        return Taste;
    }
}

ex:

@GET("/fruits")
Call<List<StrapiClass>> getListFruits()

or you can use the Gson library from https://github.com/google/gson then change the code of you:

  Response response = client.newCall(request).execute();
  String body = response.body().string();
  Type type = new TypeToken<List<StrapiClass>>() {}.getType();
  List<StrapiClass> strapiList = new Gson().fromJson(body, type);

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM