【问题标题】:How to parse a JSON without key in android?如何在android中解析没有键的JSON?
【发布时间】:2014-10-11 11:29:36
【问题描述】:

您好,我有一个简单类型的 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());

【问题讨论】:

  • 你能发布你的 CountryArray 吗?

标签: android json parsing arraylist


【解决方案1】:

您的 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"
    }
    ....
    ....
    ....
  ]
}

【讨论】:

  • 意味着我必须改变我的json?无法解析这是你的意思吗?
  • @Jims,我刚刚为您的 json 提供了正确的格式,这完全取决于您。
  • 我的问题是如何解析这个json..不改变json的格式
  • @Jims,是的,我明白了,但是 json 在格式化时解析成功。
【解决方案2】:

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

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

【讨论】:

  • 数字不是键..请仔细阅读方程式,我需要“两个”数字和国家/地区值。这些都不是键。
  • @Jims 那不是 JSON :D
  • 数字是整数。你懂我吗?
  • JSON 值 JSON 值可以是: 数字(整数或浮点) 字符串(双引号) 布尔值(真或假) 数组(方括号中) 对象(大括号中) ) 空
【解决方案3】:

我已经按照自己的方式完成了,浪费时间按照答案建议更改 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);
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-15
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多