安装包
用NuGet安装包:Newtonsoft.Json
对象转为Json字符串
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Person person = new Person { Name = "John Doe", Age = 30 };
string json2 = JsonConvert.SerializeObject(person);
Json字符串转为对象
string json = "{\"Name\":\"John Doe\",\"Age\":30}";
// 使用System.Text.Json库进行反序列化
Person person2 = JsonConvert.DeserializeObject<Person>(json);