xxxxxxxxxx
Newtonsoft.Json.JsonConvert.SerializeObject(new {foo = "bar"})
xxxxxxxxxx
using Newtonsoft.Json;
var jsonString = JsonConvert.SerializeObject(obj);
xxxxxxxxxx
var result = JsonConvert.DeserializeObject<YourClass>(jsonstring);
xxxxxxxxxx
using System;
using System.Web.Script.Serialization;
public class MyDate
{
public int year;
public int month;
public int day;
}
public class Lad
{
public string firstName;
public string lastName;
public MyDate dateOfBirth;
}
class Program
{
static void Main()
{
var obj = new Lad
{
firstName = "Markoff",
lastName = "Chaney",
dateOfBirth = new MyDate
{
year = 1901,
month = 4,
day = 30
}
};
var json = new JavaScriptSerializer().Serialize(obj);
Console.WriteLine(json);
}
}
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);
}
}