i want to get only gender but it shows {"face":[{"gender": {"value": "Male"}}]} 
i only want gender
please help me how it solve JSONArray jArray = rst.getJSONArray("face");
for (int i = 0; i < jArray.length(); i++) {
String gender=jArray.getJSONObject(i).getJSONObject("attribute").getString("gender");
String jsonText;
jsonText = gender;
textview2.setText("GENDER" + jsonText);
 
    
    - 23
- 7
- 
                    Firstly Please validate json mentioned in question on this [link](http://pro.jsonlint.com).Because JSON mentioned in question is wrong check response once. – Anil Ravsaheb Ghodake Dec 08 '16 at 11:50
- 
                    @Anil i am using api for the result – farhan Dec 08 '16 at 11:52
- 
                    Your json should be like this **{"face":[{"gender": {"value": "Male"}}]}** – Anil Ravsaheb Ghodake Dec 08 '16 at 11:54
- 
                    @Anil okay. then how can i fetch the desire value ? – farhan Dec 08 '16 at 11:56
- 
                    Check the below answer with proper json result and Let me know if any error – Anil Ravsaheb Ghodake Dec 08 '16 at 12:08
4 Answers
Check this code:-
- Case 1:This code generate only one toast - String result = "{\"face\":[{\"gender\": {\"value\": \"Male\"}}]} "; try { JSONArray objJsonArray = new JSONObject(result).getJSONArray("face"); String gender = new JSONObject(objJsonArray.getJSONObject(0).getString("gender")).getString("value"); Toast.makeText(this, "Gender is==>" + gender, Toast.LENGTH_SHORT).show(); } catch (JSONException e) { e.printStackTrace(); }
- Case 2:-This code generate two toasts. - String result = "{\"face\":[{\"gender\": {\"value\": \"Male\"}},{\"gender\": {\"value\": \"Female\"}}]}"; try { JSONArray objJsonArray = new JSONObject(result).getJSONArray("face"); for (int i = 0; i < objJsonArray.length(); i++) { String gender = new JSONObject(objJsonArray.getJSONObject(i).getString("gender")).getString("value"); Toast.makeText(this, "Gender is==>" + gender, Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { e.printStackTrace(); }
Hope it solves your problem
 
    
    - 1,587
- 2
- 27
- 45
- 
                    thanks for your solution . but your code only give's me "male" value. i want male or female from the string – farhan Dec 09 '16 at 05:10
- 
                    @farhan Its giving you the **result** as the **value of gender coming in the response**.Here I am taking your json as just example. – Anil Ravsaheb Ghodake Dec 12 '16 at 06:14
Try this on the last line:
textview2.setText("GENDER" + jsonText.value);
 
    
    - 31
- 6
- 
                    
- 
                    The result that's being returned is an object. This object is a key value pair. An example of a json object is:
 – JKyleK Dec 07 '16 at 19:52
- 
                    
- 
                    
On this line you are getting the entire array:
JSONArray jArray = rst.getJSONArray("face");<br>
A JSONArray is an Array of json objects.
The json object is a name value pair.
It looks like this: 
jArray[{name:”John” , gender:”male”}, 
{name:”jane” , gender:”female”}, 
{name:”Mike” , gender:”male”}]<br>
This next line loops through the array that you have.  Each time pulling a different json object:
1st time through = [{name:”John” , gender:”male”}
2nd time through = {name:”jane” , gender:”female”}
3rd time through = {name:”Mike” , gender:”male”}
for (int i = 0; i < jArray.length(); i++) { String 
On the first look i = 0
This following line is saying: retrieve the json data where the name is gender.
This returns {gender:”male”} 
 “gender” being the name, “male” being the value. 
gender=jArray.getJSONObject(i).getJSONObject("attribute").getString("gender").value; <br>
String jsonText; jsonText = gender; 
textview2.setText("GENDER" + jsonText);
I believe there are other more efficient ways to handle this same code, but I hope this helps none the less.
 
    
    - 31
- 6
- 
                    can you tell me how to compare json string .something like if gender is male it will show "yes it is male" in toast else "female ".. – farhan Dec 08 '16 at 05:24
- 
                    i have teried your concept but it shows error when i put value, but there is method called valueof().what should i write inside value of ? – farhan Dec 08 '16 at 10:35
try {
        JSONObject jsonObject = new JSONObject(string);
        JSONArray jsonArray = jsonObject.getJSONArray("face");
        for(int i=0; i<jsonArray.length(); i++){
            String gender = jsonArray.getJSONObject(i).getJSONObject("gender").getString("value");
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
if you have JsonArray length 1 then no need of for loop just replace i with 0
 
    
    - 359
- 5
- 19
- 
                    
- 
                    @farhan _string_ is only for your understanding. In actual it is a json response string – Piyush Dec 08 '16 at 11:14
- 
                    {"face":[{"gender":{"value":Male"}]} in this response error look properly – Dhara Patel Dec 08 '16 at 11:14
- 
                    
- 
                    W/CursorWrapperInner: Cursor finalized without prior close() 12-08 16:42:01.417 18297-18297/myproject.demofacedetection W/System.err: org.json.JSONException: No value for gender 12-08 16:42:01.417 18297-18297/myproject.demofacedetection – farhan Dec 08 '16 at 11:14
- 
                    but if this is json response then then get object why you are try to getting string – Dhara Patel Dec 08 '16 at 11:16
- 
                    
- 
                    refer this ----> http://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object?rq=1 – Dhara Patel Dec 08 '16 at 11:18
- 
                    @DharaPatel please provide me your e-mail so that i can give you my code for just simple check as i am totally new to android . well my e-mail is mohdfarhantahir@gmail.com – farhan Dec 08 '16 at 11:21
- 
                    http://www.skholingua.com/android-basic/android-app-components/resources – Dhara Patel Dec 09 '16 at 05:58