Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Newton.JSON将动态对象转换为JObject,而不是动态对象 string result=“{”AppointmentID“:463236,“Message”:“预约成功”,“Success”:true,“MessageCode”:200,“isError”:false,“Exception”:null,“ReturnedValue”:null} 动态d=JsonConvert.DeserializeObject(结果);_C#_Json_Json.net - Fatal编程技术网

C# Newton.JSON将动态对象转换为JObject,而不是动态对象 string result=“{”AppointmentID“:463236,“Message”:“预约成功”,“Success”:true,“MessageCode”:200,“isError”:false,“Exception”:null,“ReturnedValue”:null} 动态d=JsonConvert.DeserializeObject(结果);

C# Newton.JSON将动态对象转换为JObject,而不是动态对象 string result=“{”AppointmentID“:463236,“Message”:“预约成功”,“Success”:true,“MessageCode”:200,“isError”:false,“Exception”:null,“ReturnedValue”:null} 动态d=JsonConvert.DeserializeObject(结果);,c#,json,json.net,C#,Json,Json.net,d、 GetType()是Newtonsoft.Json.Linq.JObject 因此,如何将其反序列化为动态对象而不是JObject,您可能需要类似的 string result ="{"AppointmentID":463236,"Message":"Successfully Appointment Booked","Success":true,"MessageCode":200,"isError":false,"Exception":null,"ReturnedValue":null}

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


因此,如何将其反序列化为动态对象而不是JObject,您可能需要类似的

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

   dynamic d = JsonConvert.DeserializeObject<dynamic>(result);
var values=JsonConvert.DeserializeObject(json);
这可能也适合您的需要(未经测试)

dynamic d=JsonConvert.DeserializeObject(json);

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

dynamic d = JsonConvert.DeserializeObject<ExpandoObject>(json);
string result=@“{”“AppointmentID”“:463236”“Message”“:”“预约成功”“成功”“:true”“MessageCode”“:200”“iError”“:false”“Exception”“:null”“ReturnedValue”“:null}”;
动态d=JsonConvert.DeserializeObject(结果);
字符串消息=d.消息;
int code=d.MessageCode;
...

我无法直接访问属性,因为“d”不是动态对象,但它是JObject,因此它将引发异常
“d”不是动态对象
:错误
d
是一个动态对象,因为您将其声明为
dynamic
,并且可以访问任何您喜欢的属性。
JObject
类有一个很好的属性,可以自动向其属性分派调用。你有没有试过我在回答中给出的代码?这是你没有真正试过代码的第一印象,试着执行代码,你会在字符串message=d.message上出错;因为“Message”不存在,这是我的问题您是对的,我在单元测试之外执行了代码,它工作了,但在单元测试内部,它将其转换为JObject,这是非常有趣的,似乎每当我反序列化包含数组元素的结构化对象时,就会发生这种情况,例如{“id”:1,“fail”:[“val1”,“val2”]}返回JObject和运行时绑定异常中的任何后期绑定访问结果
dynamic d = JsonConvert.DeserializeObject<ExpandoObject>(json);
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;
...