org.json.JSONObject 有效 json 字符串异常

org.json.JSONObject exception on a valid json string

我有以下代码:

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

它抛出一个异常,尽管传入的 json 字符串非常有效。例外情况如下:

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

你是否发现我传递的 json 数据有问题?

顺便说一句,这是在 Eclipse watch 中看到的字符串:

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

这是一个工作版本

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);
    }
  }   
}

(使用 https://github.com/douglascrockford/JSON-java 上可用的最新版本)。我已经测试了这段代码,它编译并成功输出

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

因此,错误似乎与 json 数据无关。

毕竟是我的错。我通过 Newtonsoft 序列化程序从 .NET 程序获取数据。我错误地序列化了已经序列化的对象,结果只是一个字符串。 Eclipse中watch中的起止引号其实是值的一部分

感谢教父波尔卡的付出。