android get json数组嵌套在数组中

android get json数组嵌套在数组中,第1张

概述像thisquestion这样的东西如何保存变量?但是使用这个json代码:{"restarutant":[{"name":"HotelRaja","photo":"http:\/\/i.imgur.com\/Mzt4u.jpg","address":&qu

像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] = Chinese,second [0] = Korean,first [1] = Chinese2,second [1] = Korean2

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

解决方法:

您可以使用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();        }

现在如何从List中检索值

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: Koreanname:Hotel Raja2 First:ChIEnse2 Second: Korean2

希望这会帮助你.

总结

以上是内存溢出为你收集整理的android get json数组嵌套在数组中全部内容,希望文章能够帮你解决android get json数组嵌套在数组中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1114713.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存