Search code examples
androidjsonserializationandroid-2.2-froyoandroid-2.1-eclair

JSONObject.toString() gives different result in 2.2 than in 2.1 (.Net Date format)


In Android 2.1 this

JSONObject o = new JSONObject();
o.put("MyDate", "/Date(1289334937639)/");
Log.d(TAG, o.toString());

produces

{"MyDate":"/Date(1289334937639)/"}

but in 2.2 it produces

{"MyDate":"\/Date(1289334937639)\/"}

I am talking to a .Net web service so the 2.2 version works correctly for me. How do I make 2.1 produce the same thing without breaking 2.2?

Thanks for your help.


Solution

  • I ended up with the following:

    if (Build.VERSION.SDK_INT == 7) {
        params = params.replaceAll("/", "\\\\/");
    }
    

    where params is the json already converted to a string. Ugly, but it works.