0

i have created a rest wcf web service and hosted in local iis, the json string is converted with JsonConvert.SerializeObject(object) of Newtonsoft package.

the output of the web service is

"[{\"companyId\":2,\"companyName\":\"A\"},
{\"companyId\":8,\"companyName\":\"B\"}]"

this web service is consume by android apps, i tried with JSONArray and JSONObject but it keep on throwing exception

org.json.JSONException: Expected literal value at character 2 of   
[{\"companyId\":2,\"companyName\":\"A\"},{\"companyId\":8,\"companyName\":\"B\"}]
org.json.JSONException: Expected literal value at character 2 of 
"[{\"companyId\":2,\"companyName\":\"A\"},    
{\"companyId\":8,\"companyName\":\"B\"}]"
org.json.JSONException: Value [{"companyId":2,"companyName":"A"},
{"companyId":8,"companyName":"B"}] of type java.lang.String cannot be 
converted to JSONArray

this is the code in android class

public JSONArray RequestWebService(URL urlToRequest) {
urlConnection = (HttpURLConnection) urlToRequest.openConnection();
        urlConnection.setConnectTimeout(CONNECTION_TIMEOUT);
        urlConnection.setReadTimeout(RETRIEVE_TIMEOUT);
        urlConnection.setRequestMethod("GET");
        urlConnection.connect();

int statusCode = urlConnection.getResponseCode();

        if (statusCode == HttpURLConnection.HTTP_OK) {
            InputStream in = new BufferedInputStream(
                    urlConnection.getInputStream());
            String result = getResponseText(in);
            //result = result.substring(1, result.length() - 1);
            //result = result.replace("/\\/g", "");
            JSONArray j = new JSONArray(result);
            return j
        }
    return null;
}

private String getResponseText(InputStream inStream) throws IOException {
    StringBuilder sb = new StringBuilder();
    BufferedReader rd = null;
    try{
        rd = new BufferedReader(new InputStreamReader(inStream));
        String line;
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }
    }finally {
        if (rd != null) {
            rd.close();
        }
    }

    return sb.toString();
}
14
  • share the complete stack trace Commented Jan 28, 2016 at 8:24
  • Share your code how you tried to consume this json and show us the error message. Commented Jan 28, 2016 at 8:27
  • Hope using current json String after removing `` ? Commented Jan 28, 2016 at 8:27
  • Remove \ symbol before you convert it. Commented Jan 28, 2016 at 8:32
  • 1
    try stackoverflow.com/a/15476670/1520438 Commented Jan 28, 2016 at 8:47

1 Answer 1

0
String response = "Your Response";
try{    
    JsonArray jAry = new JsonArray(response);
        JsonObjct jObj;

        for(int i=0;i<jAry.length;i++){
            jObj = JAry.getJsonObject(i);

  // You can get your string from object jobj and also use it by store it value in arraylist.
        }
  } catch(Exception e){

}
Sign up to request clarification or add additional context in comments.

8 Comments

when i put my response in JSONArray, it throw an exception
Please use try-catch block when you parse json array.
ya, then the string will be null
If you want to get "companyId than simply you can get by jsonobj i.e jObj.optString("companyId ");
Remove \ symbol before you convert it from your webservice response
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.