0

I have the following code:

try {
    JSONObject json = new JSONObject(data);
    ...
} catch(JSONException ex) {
    if(LOGS_ON) Log.e(TAG, "Could not save data.", ex);
}

It throws an exception, although the json string passed in is pretty valid. The exception is the following:

org.json.JSONException: Value {"ShopId3Digit":"ww0","ServerTime":1426695017191,"SMSTelephone":"2104851130","SendPODAgain":true,"SendLocationAgain":true,"IsHUB":false,"AllowReceiptsAndDeliveries":true} of type java.lang.String cannot be converted to JSONObject

Do you see something wrong with the json data I'm passing in?

BTW this is the string as seen in Eclipse watch:

"{\"ShopId3Digit\":\"ww0\",\"ServerTime\":1426695017191,\"SMSTelephone\":\"2104851130\",\"SendPODAgain\":true,\"SendLocationAgain\":true,\"IsHUB\":false,\"AllowReceiptsAndDeliveries\":true}"
1
  • try using JSONParser Commented Mar 18, 2015 at 16:42

2 Answers 2

1

Here's a working version

import org.json.JSONException;
import org.json.JSONObject;

public class Test {
  public static void main(String[] args) {
    String data = "{\"ShopId3Digit\":\"ww0\",\"ServerTime\":1426695017191,\"SMSTelephone\":\"2104851130\",\"SendPODAgain\":true,\"SendLocationAgain\":true,\"IsHUB\":false,\"AllowReceiptsAndDeliveries\":true}";

    try {
      JSONObject json = new JSONObject(data);
      System.out.println("Success: json = ");
      System.out.println(json.toString(2));
    } catch(JSONException ex) {
      System.out.println("Error: " + ex);
    }
  }   
}

(using the most recent version available at https://github.com/douglascrockford/JSON-java). I have tested this code, it compiles and successfully outputs

Success: json = 
{
  "IsHUB": false,
  "SMSTelephone": "2104851130",
  "AllowReceiptsAndDeliveries": true,
  "SendPODAgain": true,
  "SendLocationAgain": true,
  "ShopId3Digit": "ww0",
  "ServerTime": 1426695017191
}

Therefore, the error seems to be not with the json data.

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

4 Comments

I'm using the version that comes with Android SDK. I even tested with the string hard-coded as you did, but I'll recheck tomorrow. I might forgot something.
Something else is happening. When I try it hard-coded it works.
@alfoks Please provide more code and information on how you obtain the json data? Can you create a minimal, self-contained example in which the error occurs?
It was my mistake after all. I obtain the data from a .NET program through Newtonsoft serializer. By mistake I was serializing the already serialized object resulting in just a string. The starting and ending quotes in the watch in Eclipse are actually part of the value.
0

It was my mistake after all. I obtain the data from a .NET program through Newtonsoft serializer. By mistake I was serializing the already serialized object resulting in just a string. The starting and ending quotes in the watch in Eclipse are actually part of the value.

Thank you godfatherofpolka for the effort you went through.

Comments

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.