记C# 通过JObject 读取 json对象(Newtonsoft.Json.Linq.JObject.this[string].get 返回 null)
作者:互联网
json对象
"RequestHeaders": {
"Host": "tool.kkbmtj.com",
"Referer": "https://m.kkbmtj.com/ys/shortindex?origin=kktj&xcx",
}
代码:
HeaderLog headerLog = new HeaderLog();
var Headerobj = (JObject)JsonConvert.DeserializeObject(RequestHeaders);
headerLog.Host = Headerobj["Host"].ToString();
headerLog.Referer = Headerobj["Referer"].ToString();
headerLog.Origin = Headerobj["Origin"].ToString();
修改为: headerLog.Origin = Headerobj.Value<string>("Origin");
提示报错
System.NullReferenceException:“未将对象引用设置到对象的实例。”
Newtonsoft.Json.Linq.JObject.this[string].get 返回 null。
标签:Origin,Newtonsoft,get,JObject,Host,headerLog,Referer,Headerobj 来源: https://www.cnblogs.com/luo0208/p/16616337.html