1

i have a rest api response in the below format

[ { "Male": "N" } ]

I need to extract the value of Male - which is N ; When i run the below code

code

public void abc() throws ClientProtocolException, IOException, JSONException  
{           
    JSONObject response = Services.getHTTPRequest(dataTable);       
    JSONArray jsonarray = new JSONArray(response);
    for (int i = 0; i < jsonarray.length(); i++) {
        response = jsonarray.getJSONObject(i);
        String flag = response.getString("Eligible Flag");
       System.out.println(flag);

}

i get below error saying : 1. org.json.JSONException: A JSONObject text must begin with '{' 2. print statement is printing the whole response. i Need just the value of Male which is N

13
  • i updated my post ; i wrote a function and trying to call it and lost .please help Commented Oct 28, 2019 at 15:33
  • lost can mean a million things. Be more specific. Please consult the Help Center to better understand how to improve your question. As it stands, the duplicates linked all explain how you can retrieve JSON objects from a JSON array. Commented Oct 28, 2019 at 15:36
  • I tried from one of the posts and not getting the desired result code: JSONObject response = Services.getHTTPRequest(dataTable); JSONArray jsonarray = new JSONArray(response); for (int i = 0; i < jsonarray.length(); i++) { JSONObject jsonobject = jsonarray.getJSONObject(i); String flag = jsonobject.getString("Male"); System.out.println(flag); iam getting the whole response not the value of Male which N Commented Oct 28, 2019 at 16:21
  • What do you think you're doing here new JSONArray(response)? Commented Oct 28, 2019 at 16:25
  • org.json.JSONException: A JSONObject text must begin with '{' - this is error iam seeing also Commented Oct 28, 2019 at 16:26

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.