C# JsonConvert.DeserializeObject在ActionResult方法中返回null
我有下一个要反序列化的Json文档:C# JsonConvert.DeserializeObject在ActionResult方法中返回null,c#,json,asp.net-mvc,json-deserialization,C#,Json,Asp.net Mvc,Json Deserialization,我有下一个要反序列化的Json文档: { "General": { "Items": [ { "fId": "divisionID", "frmt": "Text" }, { "fId": "wcctOwnerID", "frmt": "Text" }, { "fId": "qreID", "frmt": "Text"
{
"General": {
"Items": [
{
"fId": "divisionID",
"frmt": "Text"
},
{
"fId": "wcctOwnerID",
"frmt": "Text"
},
{
"fId": "qreID",
"frmt": "Text"
}
]
}
}
我有以下课程:
public class Item
{
[JsonProperty(PropertyName = "fId")]
public string fId { get; set; }
[JsonProperty(PropertyName = "frmt")]
public string frmt { get; set; }
}
public class General
{
[JsonProperty(PropertyName = "Items")]
public List<Item> Items { get; set; }
}
公共类项目
{
[JsonProperty(PropertyName=“fId”)]
公共字符串fId{get;set;}
[JsonProperty(PropertyName=“frmt”)]
公共字符串frmt{get;set;}
}
公共课普通
{
[JsonProperty(PropertyName=“Items”)]
公共列表项{get;set;}
}
我试图用这行代码反序列化:
using (StreamReader r = new StreamReader(HostingEnvironment.ApplicationPhysicalPath + @"\Utils\OptionsByDB.json"))
{
var json = r.ReadToEnd();
Utils.General items = JsonConvert.DeserializeObject<Utils.General>(json);
}
使用(StreamReader r=newstreamreader(HostingEnvironment.ApplicationPhysicalPath+@“\Utils\OptionsByDB.json”))
{
var json=r.ReadToEnd();
Utils.General items=JsonConvert.DeserializeObject(json);
}
但它返回null。我做错了什么?你的问题是你的JSON不是一个
通用的对象
是一个内有常规对象的对象:
您需要这样的类声明:
public class JsonObject{
[JsonProperty(PropertyName = "General")]
public General rootObject {get; set;}
}
然后使用:
var jsonConverted = JsonConvert.DeserializeObject<JsonObject>(json);
var jsonConverted=JsonConvert.DeserializeObject(json);
您的问题是,您的JSON不是一个通用对象
是一个内有常规对象的对象:
您需要这样的类声明:
public class JsonObject{
[JsonProperty(PropertyName = "General")]
public General rootObject {get; set;}
}
然后使用:
var jsonConverted = JsonConvert.DeserializeObject<JsonObject>(json);
var jsonConverted=JsonConvert.DeserializeObject(json);
如果调试代码,json
变量有任何值?是的,它有一个字符串和我的json文档如果调试代码,json
变量有任何值?是的,它有一个字符串和我的json文档