c# json to class code example
Example 1: object to json c#
using Newtonsoft.Json;
var jsonString = JsonConvert.SerializeObject(obj);
Example 2: string json to class c#
var result = JsonConvert.DeserializeObject<YourClass>(jsonstring);
Example 3: c# convert object to json
var jsonString = JsonConvert.SerializeObject(ObjectModel);
Example 4: string json to object c#
var resultCon = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonStr);
string json = "{\"ID\": 1, \"Name\": \"Abdullah\"}";
User user = JsonConvert.DeserializeObject<User>(json);
Example 5: json to csharp
public static T Deserialize(string serializedData)
{
var deJson = JsonConvert.DeserializeObject<T>(serializedData);
return deJson;
}
public static string Serialize(object objectName)
{
var settings = new JsonSerializerSettings()
{
ContractResolver = new OrderedContractResolver()
};
var json = JsonConvert.SerializeObject(objectName, Formatting.Indented, settings);
return json;
}
public static string Serialize(object objectName)
{
var json = JsonConvert.SerializeObject(objectName, Formatting.Indented);
return json;
}