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

1

1 Answer 1

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.

1
  • according to your suggestion. I found second solution to use JavaScriptSerializer with dynamic type. thanks. Commented Nov 20, 2013 at 3:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.