如何在android中解析没有键的JSON? [英] How to parse a JSON without key in android?

查看:21
本文介绍了如何在android中解析没有键的JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单类型的 json 响应,我已经使用键处理了 JSON,但在此响应中我只有值,请参阅下面的响应

Hi i am having a simple type of json response ,I have worked on JSON with keys but in this response i only have the values,Please see the response as below

json

{
status: "success",
country: [
{
1: "Afghanistan"
},
{
2: "Albania"
},
{
3: "Algeria"
},
{
4: "American Samoa"
},
{
5: "Andorra"
},
{
6: "Angola"
},
{
7: "Anguilla"
},
{
8: "Antarctica"
},
{
9: "Antigua and Barbuda"
},
{
10: "Argentina"
},
{.....
.
.
.
.
.so on..

所以我想解析这个 JSON 并想将两个值都放在 Hashmap 的 ArrayList 中,我的工作如下,但找不到继续进行的路径,希望有朋友能帮助我.

So i want to parse this JSON and want to put both the values in an ArrayList of Hashmap,I have worked as below,But find no path to proceed,Hope some buddy will help me.

代码

jsonObj = new JSONObject(jsonStr);
                if (jsonObj.has("country")) {

                    CountryArray = jsonObj.getJSONArray("country");
                    if (CountryArray != null && CountryArray.length() != 0) {
                        // looping through All Contacts
                        System.out
                                .println("::::::::::::::::my talent  size:::::::::::"
                                        + CountryArray.length());

推荐答案

我已经按照我的方式做了,浪费了时间按照建议的答案更改 JSON 语法,我的代码如下.

I have done it my way and wasted time in changing JSON syntax as answers suggested,my code is as below.

代码

String jsonStr = sh.makeServiceCall(allContriesURL,
                    BackendAPIService.GET);
            try {
                if (jsonStr != null) {
                    jsonObj = new JSONObject(jsonStr);
                    if (jsonObj.has("country")) {
                        CountryArray = jsonObj.getJSONArray("country");
                        if (CountryArray != null && CountryArray.length() != 0) {
                            // looping through All Contacts
                            System.out
                                    .println("::::::::::::::::my talent  size:::::::::::"
                                            + CountryArray.length());
                            for (int i = 0; i < CountryArray.length(); i++) {
                                JSONObject c = CountryArray.getJSONObject(i);
                                country_name = c.getString((i + 1) + "");
                                System.out
                                        .println(":::::::::::::::::::COUNTRY NAME:::::::::::::::::::::"
                                                + country_name);
                                HashMap<String, String> countryMap = new HashMap<String, String>();
                                countryMap.put("country_id", i + 1 + "");
                                countryMap.put("name", country_name);
                                countryList.add(countryMap);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                System.out
                        .println("::::::::::::::::::::::;;EXCEPTION::::::::::::::::"
                                + e);
            }

这篇关于如何在android中解析没有键的JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆