当我手动将此消息发送给MakeAction时,它会起作用并弹出消息框:
string json = @"{'action':'turncompleted', 'messageID':'123123123', 'playerID':'100000067174580', 'round':1, 'values':[{'longitude':21.09375, 'latitude':24.527134822598}, {'longitude':1.40625, 'latitude':23.885837699862}]}";
public void MakeAction(string message)
{
Poker_Server_v4_0.Action a = JsonConvert.DeserializeObject<Poker_Server_v4_0.Action>(message);
System.Windows.Forms.MessageBox.Show(a.action + a.messageID);
}
但是当我使用PubNub中的消息时,它不起作用:
static void DisplaySubscribeReturnMessage(string result)
{
//Receives the message from Pubnub takes care of the Action.
MessageToAction MTA = new MessageToAction();
MTA.MakeAction(result);
}
来自PubNub的消息与我手动创建的消息有点不同:
[{\"action\":\"turncompleted\",\"messageID\":\"123123123\",\"playerID\":\"100000067174580\",\"round\":1,\"answers\":[{\"longitude\":21.09375,\"latitude\":24.527134822598},{\"longitude\":1.40625,\"latitude\":23.885837699862}]},\"13926740640746402\",\"chinese-jefecito\"]
我尝试删除括号和斜线文本,但是“值”值周围的斜杠也会被替换。
我想知道我在这里做错了什么?
以下是动作类:
class Action
{
[JsonProperty("action")]
public string action { get; set; }
[JsonProperty("messageID")]
public string messageID { get; set; }
[JsonProperty("gameID")]
public string gameID { get; set; }
[JsonProperty("invitorFacebookID")]
public string invitorFacebookID { get; set; }
[JsonProperty("gametype")]
public string gametype { get; set; }
[JsonProperty("numberOfPlayers")]
public string numberOfPlayers { get; set; }
//public List<string> longitude { get; set; }
//public List<string> latitude { get; set; }
//public List<string> values { get; set; }
[JsonProperty("players")]
public List<Players> players { get; set; }
//Should be empty and removed!
[JsonProperty("values")]
public List<Value> values { get; set; }
public Action() { }
}
class Players
{
//Players
[JsonProperty("playerID")]
public string playerID { get; set; }
[JsonProperty("facebookID")]
public string facebookID { get; set; }
}
class Value
{
//Answers
[JsonProperty("longitude")]
public string longitude {get; set;}
[JsonProperty("latitude")]
public string latitude { get; set; }
}
提前致谢, 托马斯
答案 0 :(得分:1)
好吧,我只会告诉你我将使用的代码,而不是提供更正。请注意,您发布的示例json无效。我正在删除这部分; \“13926740640746402 \”,\“chinese-jefecito \”]并关闭那里的Action
个数组。你不能在json中拥有自己的密钥(两者都是没有值的密钥),并且它们也在数组中而不是在对象中。无论如何,这是我正在使用的json;
[{\ “动作\”:\ “turncompleted \”,\ “MESSAGEID \”:\ “123123123 \”,\ “playerID \”:\ “100000067174580 \”,\ “圆\”:1,\ “答案\”:[{\ “经度\”:21.09375,\ “纬度\”:24.527134822598},{\ “经度\”:1.40625,\ “纬度\”:23.885837699862}]}]
public class Action
{
public string action { get; set; }
public string messageID { get; set; }
public string round { get; set; }
public Answer[] answers { get; set; }
}
public class Answer
{
public string longitude { get; set; }
public string latitude { get; set; }
}
Action[] actions = JsonConvert.DeserializeObject<Action[]>(jsonString);