简体   繁体   English

在Android上获取JSON数组的数据

[英]Fetch data of JSON array on Android

I have a few test values in my database and i want to fetch them all in android. 我的数据库中有一些测试值,我想在android中获取它们。 This is my following JSON output of the values inside my Database: 这是数据库中值的以下JSON输出:

 [{"email_address":"rainier_gaari@hotmail.com","comment":"qwehgashdgaskdaweq","date_comment":"2014-06-21","time_comment":"08:28:00","password":"rainier1990"},
 {"email_address":"rainier_gaari@hotmail.com","comment":"asfasdasdasd","date_comment":"2104-06-12","time_comment":"09:03:00","password":"rainier1990"}
 {"email_address":"rainier_gaari@hotmail.com","comment":"asdsfafd","date_comment":"2014-06-22","time_comment":"04:44:00","password":"rainier1990"}]

But every time that I run this code: http://prntscr.com/3vhs5j , it only gives me the first line of the JSON output.: http://prntscr.com/3vi34b 但是每次我运行以下代码: http : //prntscr.com/3vhs5j时 ,它只会为我提供JSON输出的第一行。http: //prntscr.com/3vi34b

How can I show all the rows( instead of 1 row)in android? 如何显示android中的所有行(而不是1行)?

It would help if you put some of the code for getting the JSON in your question, to make it easier to reference, but, I don't see a loop, which is why it only gets the first line. 如果您在问题中放入一些用于获取JSON的代码,这将有所帮助,以使其更易于引用,但是,我看不到循环,这就是为什么它仅获得第一行的原因。

You may want to refer to this question: JSON Array iteration in Android/Java but basically use the length property of the JSONArray and loop through that many times. 您可能要参考以下问题: Android / Java中的JSON Array迭代,但基本上使用JSONArray的length属性并循环多次。

You can use a JSONObject provided by the android API. 您可以使用android API提供的JSONObject。 The JSONObject has a method called getJSONArray() JSONObject有一个称为getJSONArray()的方法

http://developer.android.com/reference/org/json/JSONObject.html#getJSONArray(java.lang.String) http://developer.android.com/reference/org/json/JSONObject.html#getJSONArray(java.lang.String)

your error there is that you are instantiating a JSONArray with your Object. 您的错误是您正在使用Object实例化JSONArray。 you should try this: 您应该尝试这样:

JSONArray myArray =jsonObject.getJSONArray();

This is a snippet of code from one of my apps that worked very well to retrieve Json data. 这是我的一个应用程序中的一段代码,这些代码很好地检索了Json数据。

 success = jObject.getInt(TAG_SUCCESS);
 vehicles = jObject.getJSONArray(Vehicle.TAG_VEHICLES);

        if(success == 1) {
            // loop through all the vehicles
            for(int i = 0; i < vehicles.length(); i++) {
                JSONObject obj = vehicles.getJSONObject(i);

                // Get each element based on it's tag
                String year = obj.getString(Vehicle.TAG_YEAR);
                String model = obj.getString(Vehicle.TAG_MODEL);
                String brand = obj.getString(Vehicle.TAG_BRAND);
                String color = obj.getString(Vehicle.TAG_COLOR);
                String license_plate = obj.getString(Vehicle.TAG_LICENSE);
                String main_driver = obj.getString(Vehicle.TAG_DRIVER);
                String policeNumber = obj.getString(Vehicle.TAG_POLICENUMBER);
                String driversLicense = obj.getString(Vehicle.TAG_DRIVER_LICENSE);
                String licenseState = obj.getString(Vehicle.TAG_LICENSE_STATE);
                String driverBirthday = obj.getString(Vehicle.TAG_BIRTHDAY_MONTH) + "/" +
                        obj.getString(Vehicle.TAG_BIRTHDAY_DAY) + "/" +
                        obj.get(Vehicle.TAG_BIRTHDAY_YEAR);
                String driverGender = obj.getString(Vehicle.TAG_DRIVER_GENDER);

                ListRowGroup group = new ListRowGroup(brand + " " + year + " " + model, brand);
                group.children.add(brand + " " + year + " " + model);
                group.children.add(policeNumber);
                group.children.add(model);
                group.children.add(color);
                group.children.add(license_plate);
                group.children.add(main_driver);
                group.children.add(driversLicense + "   -   " + licenseState);
                group.children.add(driverBirthday + "   -   " + driverGender);

                vehiclesGroup.append(i, group);
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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