Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.

2023-02-17,,,

https://blog.csdn.net/zhouyingge1104/article/details/83307637

C#项目中使用NewtonSoft.json,报错提示:

Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.

代码如下:

//{"code":200,"检测编号":"JC1810231520411","message":"OK"}
string resp = ...
JObject respObj = new JObject(resp); //报错
格式正确的json字符串却无法转换为JObject,原因是什么?

自己想明白了,原来JObject本来就不是这样创建的,正确的方法如下:

string resp = ...
JObject respObj = (JObject)JsonConvert.DeserializeObject(resp);
问题解决。
---------------------
作者:zhouyingge1104
来源:CSDN
原文:https://blog.csdn.net/zhouyingge1104/article/details/83307637
版权声明:本文为博主原创文章,转载请附上博文链接!

Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.的相关教程结束。

《Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject..doc》

下载本文的Word格式文档,以方便收藏与打印。