1

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

farhan
  • 23
  • 7

4 Answers4

1

Check this code:-

  1. 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();
    }
    
  2. 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

Anil Ravsaheb Ghodake
  • 1,587
  • 2
  • 27
  • 45
0

Try this on the last line:

textview2.setText("GENDER" + jsonText.value);

JKyleK
  • 31
  • 6
0

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.

JKyleK
  • 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
0
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

Dhara Patel
  • 359
  • 5
  • 19