xxxxxxxxxx
var result = JsonConvert.DeserializeObject<YourClass>(jsonstring);
xxxxxxxxxx
using Newtonsoft.Json;
var jsonString = JsonConvert.SerializeObject(obj);
xxxxxxxxxx
using System;
using System.Text.Json;
// Define a class representing the structure of the JSON object
public class MyObject
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Program
{
public static void Main()
{
string jsonString = "{\"Id\": 1, \"Name\": \"John\"}";
// Deserialize the JSON string to an object of MyObject
MyObject obj = JsonSerializer.Deserialize<MyObject>(jsonString);
// Access and use the object properties
Console.WriteLine("Id: " + obj.Id);
Console.WriteLine("Name: " + obj.Name);
}
}
xxxxxxxxxx
using System;
using Newtonsoft.Json;
// Define a class to convert to JSON
public class MyClass
{
public string Name { get; set; }
public int Age { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
// Create an instance of MyClass
MyClass obj = new MyClass
{
Name = "John Doe",
Age = 30
};
// Convert object to JSON string
string json = JsonConvert.SerializeObject(obj);
Console.WriteLine(json);
}
}
xxxxxxxxxx
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
public class JsonToCSharpConverter
{
public static void Main()
{
string json = @"{
'id': 1,
'name': 'John Doe',
'age': 30,
'email': 'johndoe@example.com'
}";
JObject jsonObject = JObject.Parse(json);
string className = "MyClass";
string csharpCode = GenerateCSharpClass(jsonObject, className);
Console.WriteLine(csharpCode);
}
public static string GenerateCSharpClass(JToken jsonToken, string className)
{
if (jsonToken.Type == JTokenType.Object)
{
string classCode = $"public class {className}" + Environment.NewLine;
classCode += "{" + Environment.NewLine;
foreach (JProperty property in jsonToken)
{
string propertyName = property.Name;
string propertyType = GetCSharpType(property.Value.Type);
classCode += $" public {propertyType} {propertyName} {{ get; set; }}" + Environment.NewLine;
}
classCode += "}";
return classCode;
}
return null; // Or handle other data types as per requirement.
}
public static string GetCSharpType(JTokenType tokenType)
{
switch (tokenType)
{
case JTokenType.Boolean:
return "bool";
case JTokenType.Integer:
return "int";
case JTokenType.Float:
return "double";
case JTokenType.String:
return "string";
case JTokenType.Date:
return "DateTime";
case JTokenType.Guid:
return "Guid";
case JTokenType.Null:
case JTokenType.Undefined:
return "object";
default:
return "object"; // Or handle other data types as per requirement.
}
}
}
xxxxxxxxxx
using Newtonsoft.Json;
// JSON string to be converted
string jsonString = "{\"name\":\"John\",\"age\":30}";
// Convert JSON string to object
var jsonObject = JsonConvert.DeserializeObject(jsonString);
// Access object properties
string name = jsonObject["name"].ToString();
int age = (int)jsonObject["age"];
// Print object properties
Console.WriteLine("Name: " + name);
Console.WriteLine("Age: " + age);
xxxxxxxxxx
using System;
using System.Text.Json;
public class MyClass
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreatedAt { get; set; }
}
public static void Main()
{
MyClass obj = new MyClass
{
Id = 1,
Name = "John Doe",
CreatedAt = DateTime.Now
};
string json = JsonSerializer.Serialize(obj, new JsonSerializerOptions
{
WriteIndented = true
});
Console.WriteLine(json);
}