如何从 Json 中提取以“[”开头的数据?
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,
}
]
我正在尝试从这个 json 中提取数据,这是我从 strapi API 获得的,我正在尝试在 Android
中进行
主要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();
另一个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;
}
在 运行 这段代码之后,我得到了这个默认值。
请指导我如何在 json 数组中提取正确的数据。
E/Strapi: 姓名:空
价格:0
颜色:无
口味:无
尝试使用google
的gson库
在您的应用中实施 'com.squareup.retrofit2:converter-gson:2.1.0' - build.gradle - 依赖关系
gson 的官方link:https://github.com/google/gson
更改您的代码:
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;
}}
主要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()
你可以使用 lib retrofit 从 https://square.github.io/retrofit/ 从服务器加载数据
并创建对象:
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;
}
}
例如:
@GET("/fruits")
Call<List<StrapiClass>> getListFruits()
或者您可以使用 https://github.com/google/gson 中的 Gson 库
然后更改你的密码:
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);
[
{
"id": 1,
"Name": "Banana",
"Taste": "Sweet",
"Color": "Yellow",
"Price": 4.99,
},
{
"id": 2,
"Name": "Apple",
"Taste": "Sweet",
"Color": "Red",
"Price": 5.99,
}
]
我正在尝试从这个 json 中提取数据,这是我从 strapi API 获得的,我正在尝试在 Android
中进行主要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();
另一个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;
}
在 运行 这段代码之后,我得到了这个默认值。 请指导我如何在 json 数组中提取正确的数据。
E/Strapi: 姓名:空 价格:0 颜色:无 口味:无
尝试使用google
的gson库在您的应用中实施 'com.squareup.retrofit2:converter-gson:2.1.0' - build.gradle - 依赖关系
gson 的官方link:https://github.com/google/gson
更改您的代码:
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;
}}
主要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()
你可以使用 lib retrofit 从 https://square.github.io/retrofit/ 从服务器加载数据
并创建对象:
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;
}
}
例如:
@GET("/fruits")
Call<List<StrapiClass>> getListFruits()
或者您可以使用 https://github.com/google/gson 中的 Gson 库 然后更改你的密码:
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);