1

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..

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.

code

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());
Prince
  • 496
  • 3
  • 12
Jims
  • 11
  • 3

3 Answers3

0

The "status","country" and all the "1","2", etc in the json array "country" are all keys and hence, must be in double quotes. In short, your JSON is invalid. Example:

{
"status": "success",
"country": [
{
"1": "Afghanistan"
}
]}
berserk
  • 2,690
  • 3
  • 32
  • 63
  • the numbers are not keys..Please read the quations carefully,I need the "both" number and country values .None of this is a key. – Jims Oct 11 '14 at 11:43
  • 1
    @Jims Then it is not JSON :D – berserk Oct 11 '14 at 11:44
  • numbers are integer .you getting me? – Jims Oct 11 '14 at 11:48
  • JSON Values JSON values can be: A number (integer or floating point) A string (in double quotes) A Boolean (true or false) An array (in square brackets) An object (in curly braces) null – Jims Oct 11 '14 at 12:08
0

Your Json is not correct please check below format for your requirement.

{
  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"
    }
    ....
    ....
    ....
  ]
}
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

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

code

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);
            }
Jims
  • 11
  • 3