- A Chat Room sample program created with NodeJs.
- Node.js server will response 3 different format of Json.
- I have created a Winform program to accept Json from Websocket.
- I use Json.NET JsonMessage jsonResponse = JsonConvert.DeserializeObject(e.Message.ToString()); to deSerialize one format of Node.js
- How to identify different format of Json when serialized?
three type of Json Format
color : "{\"type\":\"color\",\"data\":\"blue\"}"
message : "{\"type\":\"message\",\"action\":\"Change Color\"}"
- history : "{\"type\":\"history\",\"data\":[{\"time\":1384825833181,\"text\":\"this is test\",\"author\":\"Tom\",\"color\":\"green\"},{\"time\":1384842730192,\"text\":\"WinForm Send\",\"author\":\"WinForm Say Hello!\",\"color\":\"orange\"},{\"time\":1384842808185,\"text\":\"WinForm Send Again!!!\",\"author\":\"WinForm Say Hello!\",\"color\":\"red\"},{\"time\":1384843229766,\"text\":\"I am fine\",\"author\":\"who are you\",\"color\":\"red\"}]}"
All of the 3 Json formats can create 3 different Class with JsonProperty to map them. I can verify string with the first few characters. Are there any other solutions?
I found the following solution could help.
Use JsonCreationConverter . How to implement custom JsonConverter in JSON.NET to deserialize a List of base class objects?
Use JavaScriptSerializer with dynamic type Parse json string using JSON.NET
var jss = new JavaScriptSerializer(); dynamic data = jss.Deserialize<dynamic>(e.Message.ToString());
Use JObject.Parse with dynamic type Deserialize json object into dynamic object using Json.net