Newton.JSON将动态对象转换为JObject而不是动态对象

时间:2013-04-19 15:25:15

标签: c# json json.net

string  result ="{"AppointmentID":463236,"Message":"Successfully Appointment Booked","Success":true,"MessageCode":200,"isError":false,"Exception":null,"ReturnedValue":null}"

   dynamic d = JsonConvert.DeserializeObject<dynamic>(result);

d.GetType()是Newtonsoft.Json.Linq.JObject

所以如何将它反序列化为动态对象而不是JObject

2 个答案:

答案 0 :(得分:3)

不清楚什么不适合你以及为什么你关心返回类型但你可以直接访问反序列化对象的属性,如下所示:

string result = @"{""AppointmentID"":463236,""Message"":""Successfully Appointment Booked"",""Success"":true,""MessageCode"":200,""isError"":false,""Exception"":null,""ReturnedValue"":null}";
dynamic d = JsonConvert.DeserializeObject<dynamic>(result);

string message = d.Message;
int code = d.MessageCode;
...

答案 1 :(得分:0)

你可能想要像

这样的东西
var values = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(json);

这也可能符合您的需求(未经测试)

dynamic d = JsonConvert.DeserializeObject<ExpandoObject>(json);