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

pooja
  • 11
  • 4
  • i updated my post ; i wrote a function and trying to call it and lost .please help – pooja Oct 28 '19 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. – Sotirios Delimanolis Oct 28 '19 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 – pooja Oct 28 '19 at 16:21
  • What do you think you're doing here `new JSONArray(response)`? – Sotirios Delimanolis Oct 28 '19 at 16:25
  • org.json.JSONException: A JSONObject text must begin with '{' - this is error iam seeing also – pooja Oct 28 '19 at 16:26
  • I no longer have any idea what you're running. Edit your question to provide a [mcve]. – Sotirios Delimanolis Oct 28 '19 at 16:28
  • i have edited my question again – pooja Oct 28 '19 at 16:40
  • Is the exception being thrown within `getHTTPRequest`? If your content is what you claim, ie the JSON array `[ { "Male": "N" } ]`, then obviously it's not possible for that method to return a JSON object. – Sotirios Delimanolis Oct 28 '19 at 16:41
  • so how to proceed, the framework was already written and iam trying to understand and use it . – pooja Oct 28 '19 at 16:43
  • Change the framework or change your service. If the service is sending a JSON array, your framework should be reading a JSON array, not a JSON object. – Sotirios Delimanolis Oct 28 '19 at 16:45
  • ok the exception is resolved now . – pooja Oct 28 '19 at 16:55
  • Thanks for the help and asking questions- made me understand. again thanks for ur time . i really really appreciate – pooja Oct 28 '19 at 17:02
  • Sure thing, good luck. – Sotirios Delimanolis Oct 28 '19 at 17:22

0 Answers0