1
  1. A Chat Room sample program created with NodeJs.
  2. Node.js server will response 3 different format of Json.
  3. I have created a Winform program to accept Json from Websocket.
  4. I use Json.NET JsonMessage jsonResponse = JsonConvert.DeserializeObject(e.Message.ToString()); to deSerialize one format of Node.js
  5. How to identify different format of Json when serialized?

three type of Json Format

  1. color : "{\"type\":\"color\",\"data\":\"blue\"}"

  2. message : "{\"type\":\"message\",\"action\":\"Change Color\"}"

  3. 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.

  1. Use JsonCreationConverter . How to implement custom JsonConverter in JSON.NET to deserialize a List of base class objects?

  2. Use JavaScriptSerializer with dynamic type Parse json string using JSON.NET

    var jss = new JavaScriptSerializer();
    dynamic data = jss.Deserialize<dynamic>(e.Message.ToString());
    
  3. Use JObject.Parse with dynamic type Deserialize json object into dynamic object using Json.net

Community
  • 1
  • 1
chunhunghan
  • 51,087
  • 5
  • 102
  • 120
  • I found JsonCreationConverter could help. http://stackoverflow.com/questions/8030538/how-to-implement-custom-jsonconverter-in-json-net-to-deserialize-a-list-of-base – chunhunghan Nov 20 '13 at 02:12

1 Answers1

0

Serialized data is as string so, its just a string. As you have also told that you want to identify JSON format. So, better is first convert to JSON & then identify the JSON data type and based on the type call the process or method to process that data.