gpt4 book ai didi

android获取嵌套在数组中的json数组

转载 作者:太空宇宙 更新时间:2023-11-03 12:08:27 25 4
gpt4 key购买 nike

类似于 this question

如何保存在变量中?

但是使用这个 json 代码:

{
"restarutant": [
{
"name": "Hotel Raja",
"photo": "http:\/\/i.imgur.com\/Mzt4u.jpg",
"address": "93, 2ndc ross, GDP etx.",
"area": "Vylaikaval",
"city": "Bangalore",
"rating": "4",
"cuisines": {
"first": "Chinese",
"second": "Korean"
}
},
{
"name": "Hotel Raja2",
"photo": "http:\/\/i.imgur2.com\/Mzt4u.jpg",
"address": "93, 2ndc ross, GDP etx. number2",
"area": "Vylaikaval2",
"city": "Bangalore2",
"rating": "4",
"cuisines": {
"first": "Chinese2",
"second": "Korean2"
}
}
]
}

代码:

JSONObject json = new JSONObject(thepreviousjson);
JSONArray jArray = json.getJSONArray("restaurant");

String name[] = new String[jArray.length()];
String photo[] = new String[jArray.length()];
String address[] = new String[jArray.length()];
String area[] = new String[jArray.length()];
String city[] = new String[jArray.length()];
String rating[] = new String[jArray.length()];

String cuisines[] = new String[jArray.length()];
String first[] = new String[jArray.length()];
String second[] = new String[jArray.length()];

for(int i=0; i<jArray.length(); i++){
JSONObject json_data = jArray.getJSONObject(i);

name[i] = json_data.getString("name");
photo[i] = json_data.getString("photo");
address[i] = json_data.getString("address");
area[i] = json_data.getString("area");
city[i] = json_data.getString("city");
rating[i] = json_data.getString("rating");
}

关键是要存储:
name[0] = "Hotel Raja"...
name[1] = "Hotel Raja2"
first[0] = 中文,second[0] = 韩文,first[1] = 中文2,second[1] = 韩文2

我尝试了几种组合,但没有任何反应,我的代码中需要修改什么?谢谢

最佳答案

您可以使用 List 而不是 String Array

最好使用 Model Concept。

步骤:1)

为 Restarutant.java 创建模型

public class Restarutant {
private String name;
private String photoUrl;
private String address;
private String area;
private String city;
private int rating;
private Cuisines cuisines;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhotoUrl() {
return photoUrl;
}
public void setPhotoUrl(String photoUrl) {
this.photoUrl = photoUrl;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getRating() {
return rating;
}
public void setRating(int rating) {
this.rating = rating;
}
public Cuisines getCuisines() {
return cuisines;
}
public void setCuisines(Cuisines cuisines) {
this.cuisines = cuisines;
}

}

步骤:2) 为美食创建模型

public class Cuisines {
private String first;
private String second;
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getSecond() {
return second;
}
public void setSecond(String second) {
this.second = second;
}
}

最后一步:现在如何在解析后将数据存储在模型中

List<Restarutant> list = new ArrayList<Restarutant>();
try {
JSONObject json = new JSONObject(thepreviousjson);
JSONArray jArray = json.getJSONArray("restaurant");
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Restarutant data = new Restarutant();// Create Object Here
data.setName(json_data.getString("name"));
data.setPhotoUrl(json_data.getString("photo"));
data.setAddress(json_data.getString("address"));
data.setArea(json_data.getString("area"));
data.setCity(json_data.getString("city"));
JSONObject cuisines = json_data.getJSONObject("cuisines");
Cuisines cuisine = new Cuisines();// Create Object here
cuisine.setFirst(cuisines.getString("first"));
cuisine.setSecond(cuisines.getString("second"));
data.setCuisines(cuisine);// setting the cuisine
list.add(data);// Finally adding the model to List

}

} catch (JSONException e) {
e.printStackTrace();
}

现在如何从列表中检索值

for(int i=0;i<list.size();i++){
Restarutant restarutant=list.get(i);
String name=restarutant.getName();
String first=restarutant.getCuisines().getFirst();
String second=restarutant.getCuisines().getSecond();
System.out.println("Name: "+name+"First "+first+"Second "+second);
}

输出:

Name:Hotel Raja First:Chiense Second: Korean
Name:Hotel Raja2 First:Chiense2 Second: Korean2

希望对您有所帮助。

关于android获取嵌套在数组中的json数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19748829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com