Json.net实现方便的Json转C#(dynamic动态类型)对象
作者:互联网
public class Person {
public int Id { get; set; }
public string Name { get; set; }
}
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.Id = 1;
p.Name = "刘备";
//C#对象转Json
string json = JsonConvert.SerializeObject(p);
Console.WriteLine(json); //{"Id":1,"Name":"刘备"}
//此处模拟在不建实体类的情况下,反转将json返回dynamic对象
var DynamicObject = JsonConvert.DeserializeObject<dynamic>(json);
Console.WriteLine(DynamicObject.Name); //刘备
Console.ReadKey();
}
}
static void Main(string[] args) { string json = "{\"Id\":1,\"Name\":\"刘备\",\"Age\":\"22\"}"; //此处模拟在不建实体类的情况下,反转将json返回回dynamic对象 var DynamicObject = JsonConvert.DeserializeObject<dynamic>(json); Console.WriteLine(DynamicObject.Name); //刘备 Console.ReadKey(); }
标签:Console,Name,C#,dynamic,json,Json,DynamicObject,Id,string 来源: https://www.cnblogs.com/akxmhd/p/15630367.html