无法反序列化json字符串C#

时间:2015-11-05 10:12:44

标签: c# json json.net deserialization

我使用GCM客户端向我的服务器发送json数据到Android。以下是我的代码:

服务器:

Dictionary<string, string> dataDict = new Dictionary<string, string>();
dataDict.Add("Text", Convert.ToString(dr["Message"]));
dataDict.Add("Type", Convert.ToString(dr["Type"]));

GcmJsonData jsonData = new GcmJsonData();
jsonData.alert = dataDict;
jsonData.sound = "sound.caf";
var json = new JavaScriptSerializer().Serialize(jsonData);
try
{
    //GcmNotification n = new GcmNotification();
    _push.QueueNotification(new GcmNotification().ForDeviceRegistrationId("APA91bFoQBrmsBeoXSdU4-UdO30Wn")
        .WithJson(json));
}

机器人:

string msgFromServer = "";
if (intent != null || intent.Extras != null)
{
    msgFromServer = intent.GetStringExtra("alert");
    string t = intent.GetStringExtra("Name");
}

msgFromServer = msgFromServer.Replace(@"\""", @"""");
Data d = Newtonsoft.Json.JsonConvert.DeserializeObject<Data>(msgFromServer);

收到msgFromServer如下:

"{\"Type\":\"GeneralAlert\",\"Text\":\"ga a\"}"

因此我无法反序列化它。

如何修复

0 个答案:

没有答案