-2

How to get the value by key in a JSON object? I had used following code but it receives "org.json.JSONException". Advance thanks for any help.

String resultJSON = "{Data:[{\"AreaID\":\"13\", \"Phone\":\"654321\", \"RegionName\":\"Sivakasi\"}, {\"AreaID\":\"14\", \"Phone\":\"12345\", \"RegionName\":\"ANJAC\"}]}";
                 JSONObject jObject  = new JSONObject(resultJSON);
                    JSONObject  jsonObject = jObject.getJSONObject("Data");


Map<String,String> map = new HashMap<String,String>();
            Iterator iter = jsonObject.keys();
            while(iter.hasNext()){
                String key = (String)iter.next();
                String value = jsonObject.getString(key);
                map.put(key,value);
                Log.d("Key Value","key: "+key+" Value: "+value);
            }

Logcat details

   org.json.JSONException: Value [{"AreaID":"13","Phone":"654321","RegionName":"Sivakasi"},{"AreaID":"14","Phone":"12345","RegionName":"ANJAC"}] at Data of type org.json.JSONArray cannot be converted to JSONObject
Sakthimuthiah
  • 2,606
  • 6
  • 26
  • 41

2 Answers2

7

The structure of your JSON is wrong, you should use a Key for the second JSONObject , like this:

{
    Data: {
        \"AreaID\": \"13\",
        \"Phone\": \"654321\",
        \"RegionName\": \"Sivakasi\"
    },
    \"KEY\": {
        \"AreaID\": \"14\",
        \"Phone\": \"12345\",
        \"RegionName\": \"ANJAC\"
    }
}

Or the DATA should be a JSONArray ( surrounded by [] ) like this :

{
    Data: [
        {
            \"AreaID\": \"13\",
            \"Phone\": \"654321\",
            \"RegionName\": \"Sivakasi\"
        },
        {
            \"AreaID\": \"14\",
            \"Phone\": \"12345\",
            \"RegionName\": \"ANJAC\"
        }
    ]
}

NOTE : you can check if your json is valid or not here

Personnaly , i prefer the second way ( Using JSONArray) , because the data inside has the same attributes (AreaID, Phone, REgionName). To parse data in this case , your code should be someting like this :

String resultJSON = "{Data:[{\"AreaID\":\"13\", \"Phone\":\"654321\", \"RegionName\":\"Sivakasi\"}, {\"AreaID\":\"14\", \"Phone\":\"12345\", \"RegionName\":\"ANJAC\"}]}";
                 JSONObject jsonRoot  = new JSONObject(resultJSON);
                    JSONArray  jsonData = jsonRoot.getJSONArray("Data");
    for(int i=0; i<jsonData.lenght;i++) {
        JSONObject jsonOBject = jsonData.getJSONObject(i);
        Log.d(TAG, "json ("+i+") = "+jsonOBject.toString());
        // do what you want with your JSONObject , i.e :add it to an ArrayList of paresed result
        String areaID = jsonOBject.getString("AreaID");
        int phoneNumber = jsonOBject.getInt("Phone");
        String regionName = jsonOBject.getString("RegionName");
    }
ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58
Houcine
  • 24,001
  • 13
  • 56
  • 83
  • Yeah that's my mistake in question. Now i had updated my question – Sakthimuthiah Sep 27 '13 at 11:32
  • @Sakthimuthiah : i've updated my answer too , which value you want to retreive from your JSON ?? – Houcine Sep 27 '13 at 11:32
  • here is my json format and i am getting json string as given below.but i want to get particular value,how to get it? {"login":[{"status":"error"}]} –  Oct 28 '14 at 11:34
  • Eddie : You will get the JSONArray 'login' , and then you will get the first JSONObject of that JSONArray. and then , getString('status'). it is just like my answer , the only difference is that you have one jsonObject in your jsonArray. That's it – Houcine Oct 28 '14 at 12:06
1

This is invalid JSON format. Before you convert your string to JSON object format, be sure about it's valid or not.

Please check validity of your JSON.

Hope it may help.

gokhanakkurt
  • 4,935
  • 4
  • 28
  • 39