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

用户名

这个问题

如何保存变量?

但是使用以下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] = Chinese, second[0] = Korean, first[1] = Chinese2, second[1] = Korean2

我尝试了几种组合,但是没有任何反应,我需要在代码中进行哪些修改?谢谢

阿米特·古普塔(Amit Gupta)

您可以使用List代替String Array

更好地使用模型概念。

步骤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

希望这会帮助你。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Firebase获取嵌套在数组中的数据

来自分类Dev

使用python打印嵌套在数组中的json对象

来自分类Dev

Mongodb:查询嵌套在数组中的json对象

来自分类Dev

Mongodb:查询嵌套在数组中的json对象

来自分类Dev

计算嵌套在数组中的可观察数组的和

来自分类Dev

Mongodb 展开数组嵌套在数组中

来自分类Dev

遍历嵌套在JSON中的Results数组

来自分类Dev

访问嵌套在数组内的JSON对象

来自分类Dev

我正在尝试使用json数据更新嵌套在数组中的对象中的状态...

来自分类Dev

如何使用NSDictionary收集嵌套在数组中的JSON数据

来自分类Dev

通过猫鼬删除嵌套在数组中的文档

来自分类Dev

替换嵌套在数组中的哈希的键值

来自分类Dev

Ruby-拒绝嵌套在数组中的哈希

来自分类Dev

将AnyObject转换为嵌套在数组中的Dictionary

来自分类Dev

嵌套在数组中的Javascript代表问题

来自分类Dev

嵌套在数组中的 Ruby Sinatra 哈希

来自分类Dev

反序列化嵌套在数组中的对象

来自分类Dev

访问嵌套在 JSON 对象中的数组中的对象值

来自分类Dev

如何从嵌套在数组对象内部的数组中删除重复的对象值?

来自分类Dev

使用.join()无法连接嵌套在数组内部对象中的数组

来自分类Dev

如何调用方法并检索嵌套在flutter中的json数组

来自分类Dev

如何在mongodb集合中搜索嵌套在数组中的字典键

来自分类Dev

如何解码circe中嵌套在数组中的Date?

来自分类Dev

如何为嵌套在数组中的文档中的字段设置唯一约束?

来自分类Dev

PySpark:如何从嵌套在数组内部结构中的结构中提取变量?

来自分类Dev

猫鼬检查id是否存在,但该ID嵌套在数组中

来自分类Dev

合并嵌套在数组中的哈希,同时仅使用不同的键

来自分类Dev

使用 Doctrine mongodb odm 查询嵌套在数组中的文档

来自分类Dev

如何通过其键之一的值访问嵌套在数组中的对象?

Related 相关文章

  1. 1

    使用Firebase获取嵌套在数组中的数据

  2. 2

    使用python打印嵌套在数组中的json对象

  3. 3

    Mongodb:查询嵌套在数组中的json对象

  4. 4

    Mongodb:查询嵌套在数组中的json对象

  5. 5

    计算嵌套在数组中的可观察数组的和

  6. 6

    Mongodb 展开数组嵌套在数组中

  7. 7

    遍历嵌套在JSON中的Results数组

  8. 8

    访问嵌套在数组内的JSON对象

  9. 9

    我正在尝试使用json数据更新嵌套在数组中的对象中的状态...

  10. 10

    如何使用NSDictionary收集嵌套在数组中的JSON数据

  11. 11

    通过猫鼬删除嵌套在数组中的文档

  12. 12

    替换嵌套在数组中的哈希的键值

  13. 13

    Ruby-拒绝嵌套在数组中的哈希

  14. 14

    将AnyObject转换为嵌套在数组中的Dictionary

  15. 15

    嵌套在数组中的Javascript代表问题

  16. 16

    嵌套在数组中的 Ruby Sinatra 哈希

  17. 17

    反序列化嵌套在数组中的对象

  18. 18

    访问嵌套在 JSON 对象中的数组中的对象值

  19. 19

    如何从嵌套在数组对象内部的数组中删除重复的对象值?

  20. 20

    使用.join()无法连接嵌套在数组内部对象中的数组

  21. 21

    如何调用方法并检索嵌套在flutter中的json数组

  22. 22

    如何在mongodb集合中搜索嵌套在数组中的字典键

  23. 23

    如何解码circe中嵌套在数组中的Date?

  24. 24

    如何为嵌套在数组中的文档中的字段设置唯一约束?

  25. 25

    PySpark:如何从嵌套在数组内部结构中的结构中提取变量?

  26. 26

    猫鼬检查id是否存在,但该ID嵌套在数组中

  27. 27

    合并嵌套在数组中的哈希,同时仅使用不同的键

  28. 28

    使用 Doctrine mongodb odm 查询嵌套在数组中的文档

  29. 29

    如何通过其键之一的值访问嵌套在数组中的对象?

热门标签

归档