How can we create an anonymous object from json string in c#? - c#

My JSON string will be like
when I deserialize it using Newtonsoft.Json, instead of object I am getting below response
But it should be like below image
tried JsonConvert.DeserializeObject and JObject.Parse. is there any way to get in direct object structure when we deserialize it without knowing type?

Try a dynamic? e.g.
dynamic thing = JObject.Parse("{Id:123, PhoneNumber: { Primary: 12345, Secondary: 78945}");
Console.WriteLine(thing.Id);
Console.WriteLine(thing.PhoneNumber);
Console.WriteLine(thing.PhoneNumber.Primary);

Related

How could I access to a Dictionary objects?

I have a dictionary parsed from a JSON (more information here Can't deserialize Dictionary from JSON) and finally could get the parse done. I get an object with this structure shown in the image. How can I access the values from this object curContent? The "natural" way was trying to cast curContent.soportes[0].avisos to Aviso[] but it says it cannot cast from jArray to Aviso[].
currContent.soportes[2].Value
but this will give you the json string it looks like. You'd need to deserialize that as well

Deserialize JSON Array with JSON.NET JArray

I'm trying to deserialize my JSON Array using Newtonsoft JSON.NET nugget:
Here's the code:
private List<TemplateTypesObj> getTemplateTypes(JArray array)
{
List<TemplateTypesObj> templateTypes = Newtonsoft.Json.JsonConvert.DeserializeObject<List<TemplateTypesObj>>(array);
return templateTypes;
}
The only issue is that DeserializeObject takes String, not an JArray object. I can do array.toString() but I'm not sure if that is a proper way to do that.
That's because a JArray doesn't really need deserializing. It's not a string/binary representation of an object (which is something you'd deserialize). It's already an object which represents your JSON. You can use it like an object - iterate through it, extract individual items from it.
Check out the docs at http://www.newtonsoft.com/json/help/html/t_newtonsoft_json_linq_jarray.htm - there are methods in there which I'm sure could be used to achieve the conversion you want.

Array of Objects in C# wont serialize to JSON

I am having some trouble getting an array of objects in C# to serialize to JSON.
My Code:
friendlyNotification[] notifications = ns.friendlyNotifications.ToArray();
string json = JsonConvert.SerializeObject(notifications);
return Json(json, JsonRequestBehavior.AllowGet);
The code above return the following:
[{}]
Does anyone have and idea why this isn't working? Something to do with the fact that I am trying to serialize an array?
Thanks.
As friendlyNotification is a class without public properties, and notifications contains only one element the json result is [{}].
The json [{}] has the meaning, of an array with an empty object.

Json.Net Parse json of different format

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.

Looking to create JSON Serializer/Deserializer Class using C# 2.0

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.

Categories