如何解析没有在android系统密钥的JSON?(How to parse a JSON witho

2019-10-21 03:00发布

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

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


Answer 2:

“状态”,“国家”,所有的“1”,“2”等的JSON阵列中的“国家”是所有的键,因此,必须是在双引号。 总之,你的JSON是无效的。 例:

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


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


文章来源: How to parse a JSON without key in android?