在API响应上获取JSONElement

时间:2018-06-08 03:22:22

标签: android json android-networking

我正在使用Android Networking并使用get,我获取数据,这是我正在调用的API。

String url =  baseUrl + "atts_devices?device_serial=eq." + Build.SERIAL + "&select=*,subcompany_id{name}"

并得到这样的回复

 [{"permit_to_use":null,"min":null,"company_id":"4ed8954c-703e-41b5-94ba-eeb29546e3e3","model":"q1","bus_id":"b6d52ce3-3672-4230-9f9e-a6a465c1c8ea","sam_card":"04042DE2E52080","sim_number":null,"device_serial":"WP18121Q00000410","created_on":"2018-05-24T13:28:28.251004+00:00","remarks":null,"location_id":"ec176824-4cb7-4376-a436-429b529f8b45","id":"36bc07be-7d93-4209-ba15-9c9da7a58c3c","device_name":"wizarpos-ken","is_activated":"1","qrcode":null,"is_assigned":"1","sd_serial":"1510124e436172641068a36718010b00","updated_on":"2018-06-07T09:18:29.365416+00:00","software_serial":null,"subcompany_id":{"name":"MAN LINER"},"sim_serial":"89630317324030972665"}]

请注意subcompany_id有另一个JSONArray。我怎样才能获得name内部的内容?

我正在使用Android Fast Networking,这是整个过程

AndroidNetworking.get(baseUrl + "atts_devices?device_serial=eq." + Build.SERIAL + "&select=*,subcompany_id{name}")
            .setPriority(Priority.HIGH)
            .build()
            .getAsJSONArray(new JSONArrayRequestListener() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.e("response", String.valueOf(response));
                    try {
                        for (int i = 0; i < response.length(); i++) {
                            JSONObject jresponse = response.getJSONObject(i);

                            String raw_id = jresponse.get("id").toString();
                            String id_string = raw_id;
                            id_string = id_string.replace("\"", "");
                            String id_final = String.valueOf(id_string);

                            String raw_company_id = jresponse.get("company_id").toString();
                            String company_id_string = raw_company_id;
                            company_id_string = company_id_string.replace("\"", "");
                            String company_id_final = String.valueOf(company_id_string);

                            String raw_subcompany_id = jresponse.get("subcompany_id").toString();
                            String subcompany_id_string = raw_subcompany_id;
                            subcompany_id_string = subcompany_id_string.replace("\"", "");
                            String subcompany_id_final = String.valueOf(subcompany_id_string);


                            String raw_location_id = jresponse.get("location_id").toString();
                            String location_id_string = raw_location_id;
                            location_id_string = location_id_string.replace("\"", "");
                            String location_id_final = String.valueOf(location_id_string);

                            String raw_bus_id = jresponse.get("bus_id").toString();
                            String bus_id_string = raw_bus_id;
                            bus_id_string = bus_id_string.replace("\"", "");
                            String bus_id_final = String.valueOf(bus_id_string);

                            CompanyID = company_id_final;
                            BusID = bus_id_final;
                            subCompID = subcompany_id_final;

                            dbmanager.insertToDeviceInformation(id_final,
                                    company_id_final,
                                    subcompany_id_final,
                                    location_id_final,
                                    bus_id_final);
                        }


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

2 个答案:

答案 0 :(得分:2)

创建一个新的JSONObject JSONObject response = jresponse.get("subcompany_id"),然后初始化您的String name = response.get("name")

答案 1 :(得分:0)

"subcompany_id":{"name":"MAN LINER"}

这是JSONObject而非JSONArrayJSONArray[{ }]开头

所以你可以像JSONObject那样得到“名字”。

JSONObject jsonObjectSubCompanyId = jresponse.getJSONObject("subcompany_id");
String name = jsonObjectSubCompanyId.getString("name");