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
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.
Related
I receive flattened json properties in the following format:
jsonName.propertyPath1.propertyPath2..., value
Example:
UniversityCollection.Persons.Person1.Name, Dave
I need to validate these kind of properties with a JSchema, that a receive from a file before generating a JObject with all the properties. I just have the program that collect them and generate the JObject.
It is important to note that i do not have knowledge about neither the structure of the Json nor the Json schema. I want to make a general program that validates any Json received through its associated Json schema.
I have have a json url that i would like to extract data from, I am just new to doing this. What I have attempted to do is to copy the the json data and get it to a class. Now I want to use the json data from the url to put it into variables created on that class and display it on standard output.
Here is what I have done:
You will need to override the ToString method on the Current_Observation object to output object properties to the console.
I have some JSON data :-
{
"mail":"mitch#domain.com",
"givenName":"User",
"sn":"Name",
"uid":"mitch",
"gecos":"User Name"
}
What I'm trying to do is de-serialize this into a List<KeyValuePair<string,string>>
I would normally do a dictionary, however some key's may be duplicated - this is the representation that is automatically generated by .NET when I pass a List<KeyValuePair<string,string>> object into the System.Web.Script.Serialization.JavaScriptSerializer class.
When I just plug the serialized object into the System.Web.Script.Serialization.JavaScriptDeserializer I get a empty response back.
From what I can see it should not be possible using the JavaScriptSerializer. The only way for customizing its behavior is by means of a JavaScriptConverter class, that will allow you to customize the serialization/deserialization process. Unfortunately both methods will pass an IDictionary for the properties, therefore the duplicated names are already merged. You might want to look into either a different format for your JSON or a different serialization library such as JSON.net which is way more customizable.
I have a winform application that contains a datagridview that I would like to populate using a JSON webservice. I am not having much luck finding any examples on the web, is this possible?
The webservice can output XML or JSON and the XML is no problem but I feel that the JSON would provide a quicker response.
You can create a class to mimic the json structure and then use the javascript serializer to convert json response to a bindable list
var myList = new JavaScriptSerializer().Deserialize<IList<MyClass>>(json);
This google search gives you some complete examples about how to deserialize a json object using the JavaScriptSerializer class
I am using c#.
Now I am looking to create a "JSONHelper" class, which will Serialize and Deserialize the JSON string data. In my current scenario, I am getting below format string from my web-service which returns JSON type.
I have got a method in my C# code which calls a web-service method, which returns the string JSON type data. See below example.
string userDetails = myWebService.GetMember(username, Password, out Result);
so userDetails value is
{"FullName":"Manoj Singh","username":"Manoj","Miles":2220,"TierStatus":"Gold","TierMiles":23230,"MilesExpiry":12223,"ExpiryDate":"31 January 2011","PersonID":232323,"AccessToken":"sfs23232s232","ActiveCardNo":"232323223"}
Now I want to write JSONHelper Class in which there will be methods to Serialize and Deserialize the string type of JSON, and will return the dictionary type of values.
Have you looked at http://json.codeplex.com/ ?
I've used it and find json.net very good. Not sure why you'd want to write your own.
You can use the JavaScriptSerializer class in the System.Web.Script.Serialization namespace:
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
object result = jsonSerializer.DeserializeObject(jsonString);
Assuming your jsonString represents a object this should return a dictionary. But you will need to check the type carefully as there is no guarantee it won't return an IEnumerable for example if you give it a json list.
Better still you can use a strongly type deserialization:
http://pietschsoft.com/post/2008/02/NET-35-JSON-Serialization-using-the-DataContractJsonSerializer.aspx
If you know the return signature of your web method this is a far better solution.