I have a a JObject and I would like to set a property from a strongly typed object on it.
JObject["ProductionVersion"] = new ProductionVersion();
In order to do this, ProductVersion needs to be converted to a JToken. How can I do this without having to serialize and deserialize the object as a JObject?
JObject["ProductVersion"] = JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeObject(message.ProductVersion))
Your question is a bit confusing..
So you have a JObject and you want a JToken?
Well, a JObject is a JToken. Take a look at the inheritance hierarchy here: JObject class
If what you meant is "I have a serializable object, and I want to convert it to a JToken without having to serialize and deserialize it again", then use this JToken.FromObject(obj)
You can access the properties by calling Properties on the JObject.
When you need to add a property, just add it using the JProperty constructor.
See the JSON.NET documentation.
Related
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
Is there a nice way to get a json value, using var myValue = json["prop"] and insert it to a common object/interface? The value could be a json {} or an array []. I know I can insert them to a JObject and JArray, but is there a common object?
I also want to know (maybe the same answer to the above), if I can parse json from string, when again, I don't know if it's an array or an object.
The JToken type is a common base type for JObject and JArray. It is what json["prop"] would return, and if you had a JToken of either type, then you could set json["prop"] = token.
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.
I am using Newtonsoft.Json to serialize/deserialize my C# object to/from JSON.
My C# object has a property of following type:
List<BaseClass> objects { get; set; }
This collection holds different child objects (e.g. - ChildClass1, ChildClass2).
Serializing to JSON works fine but while deserializing it creates objects of BaseClass in collection (which is obvious :)).
Is there a way I can create concrete child objects while deserializing?
I tried creating a custom converter by implementing CustomCreationConverter<BaseClass>. But the problem is overridden Create method gives me just the type and based on that I cannot decide which object to return (ChildClass1 or ChildClass2)
My JSON string has a Type property by which I can identify the child object to create.
Any help is highly appreciated.
You can use the TypeNameHandling option:
var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }
JsonConvert.SerializeObject(myObject, Formatting.Indented, settings );
The TypeNameHandling option "Auto" ensures that type names are included in the serialized JSON
for subclasses. But watch out: these names become invalid when the classes or their namespaces are renamed!
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.