我使用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\"}"
因此我无法反序列化它。
如何修复