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();
}
上一篇:调出重复问题


下一篇:P2617 Dynamic Rankings