-1

I am using following code to deserailze the JSON data from Url. But My Error is:Value {"InvoiceNo":18} of type java.lang.String cannot be converted to JSONObject.My Json Like: "{\"InvoiceNo\":18}" Please Any one Help me.

    private class LongOperation extends AsyncTask{
    @Override
    protected Object doInBackground(Object[] params) {
        HttpResponse response = null;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            request.setURI(new URI(
                    "http://192.168.1.2/Json/api/test"));
            response = client.execute(request);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }

        String responseText = null;
        try {
            responseText = EntityUtils.toString(response.getEntity());


        } catch (IOException e) {

            e.printStackTrace();


        }
        try {
            JSONObject json = new JSONObject(responseText); // **Error on this line**
            Iterator<String> keys = json.keys();

            while (keys.hasNext()) {
                String key = keys.next();
                String value = null;
                try {
                    value = json.getString(key);
                    Toast.makeText(Billing.this, value + "",
                            Toast.LENGTH_SHORT).show();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                 textInvoice.setText(value.toString());
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.i("responseText", responseText);

        return null;
    }
}
7
  • 2
    not valid JSON then? Mar 11, 2016 at 7:30
  • I have already checked this is valid json and hardcode this json string works fine @ScaryWombat Mar 11, 2016 at 7:33
  • print out the value of responseText before new JSONObject(responseText); Mar 11, 2016 at 7:36
  • My print out value is:responseText: "{\"InvoiceNo\":18}" @ScaryWombat Mar 11, 2016 at 7:39
  • I do not understand what quotes are involved. Mar 11, 2016 at 7:42

1 Answer 1

1

Your String should not contain the character "\". If so, this is the cause of your problem.

3
  • I know I should post comments rather than an answer. But I don't have enough reputations to do so : ( Sorry for that.
    – Weizhou He
    Mar 11, 2016 at 9:32
  • Reviewers please check meta.stackexchange.com/questions/185073/… before deciding
    – Tim
    Mar 11, 2016 at 9:39
  • @WeizhouHe: Please don't misuse answers for other purposes if you don't have enough reputation yet. Anyway, if you think that you have found the problem, then please don't let your answer sound like a clarification question. Please do it similar to Tunaki, who edited your answer.
    – honk
    Mar 11, 2016 at 9:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.