如何在android中解析没有键的JSON?

时间:2014-10-11 11:29:37

标签: android json parsing arraylist

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

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中,我的工作方式如下,但是找不到继续的路径,希望有些伙伴会帮助我。

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());

3 个答案:

答案 0 :(得分:0)

json数组“country”中的“status”,“country”和所有“1”,“2”等都是键,因此必须是双引号。简而言之,您的JSON无效。 例如:

{
"status": "success",
"country": [
{
"1": "Afghanistan"
}
]}

答案 1 :(得分:0)

您的Json不正确请根据您的要求查看以下格式。

{
  status: "success",
  country: [
    {
      "name": "Afghanistan",
      "value": 1
    },
    {
      "name": "Albania",
      "value": 2
    },
    {
      "name": "Algeria",
      "value": 3
    },
    {
      "name": "American Samoa",
      "value": 4
    },
    {
      "name": "Andorra",
      "value": 5
    },
    {
      "name": "Angola",
      "value": "6"
    }
    ....
    ....
    ....
  ]
}

答案 2 :(得分:0)

我已经按照自己的方式完成了工作,浪费了时间来更改JSON语法作为答案建议,我的代码如下所示。

<强>码

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);
            }