将 Json 字符串反序列化为 C# 模型
Deserialize Json string to C# model
我有以下 JSON 对象,我可以访问我的 Web API 控制器:
{
"id": "13",
"title": "party",
"periods": {
"0": {
"label": "Period",
"start_date": "2015-04-20",
"end_date": "2015-04-29"
}
}
}
我想尝试将其直接反序列化为我在 C# 中拥有的模型,但我也做不到。
这是我的模型:
public class PeriodsModel
{
[JsonProperty("id")]
public int id { get; set; }
[JsonProperty("title")]
public string title { get; set; }
[JsonProperty("periods")]
public Periods periods { get; set; }
}
public class Periods
{
[JsonProperty("0")]
public Dictionary<string,Period> period { get; set; }
}
public class Period
{
[JsonProperty("label")]
public string label { get; set; }
[JsonProperty("start_date")]
public string start_date { get; set; }
[JsonProperty("end_date")]
public string end_date { get; set; }
}
这是我在控制器中的方法:
public void Put([FromBody]JToken jsonbody)
{
var myJsonObject = JsonConvert.SerializeObject(jsonbody);
PeriodsModel model = JsonConvert.DeserializeObject<PeriodsModel>(myJsonObject);
}
这是我收到的错误消息:
An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code
Additional information: Error converting value "Period" to type 'CMS.WebApi.Controllers.ActivitiesController+Period'. Path 'periods.0.label', line 1, position 62.
您可以这样做,因为“0”更像是一个键而不是 属性 变量:
public class PeriodsModel
{
[JsonProperty("id")]
public int id { get; set; }
[JsonProperty("title")]
public string title { get; set; }
[JsonProperty("periods")]
public Dictionary<string, Period> periods { get; set; }
}
public class Period
{
[JsonProperty("label")]
public string label { get; set; }
[JsonProperty("start_date")]
public string start_date { get; set; }
[JsonProperty("end_date")]
public string end_date { get; set; }
}
错误信息是正确的。稍微深入研究数据...您有:
"0": {
"label": "Period",
"start_date": "2015-04-20",
"end_date": "2015-04-29"
}
你试着把它放进去:
public class Periods
{
[JsonProperty("0")]
public Dictionary<string,Period> period { get; set; }
}
public class Period
{
[JsonProperty("label")]
public string label { get; set; }
[JsonProperty("start_date")]
public string start_date { get; set; }
[JsonProperty("end_date")]
public string end_date { get; set; }
}
更具体地说,这个 JSON:
{
"label": "Period",
"start_date": "2015-04-20",
"end_date": "2015-04-29"
}
正在反序列化为 Dictionary<string,Period>
那么第一个属性:
"label": "Period"
需要反序列化为 <string, Period>
对。
左侧 "label"
正确转换为 string
但右侧是字符串 "Period"
,并且该字符串无法反序列化为 class 类型 Period
因此出现错误
Error converting value "Period" to type 'CMS.WebApi.Controllers.ActivitiesController+Period'
它正在尝试将字符串 "Period" 转换为 class 句点。
目前还不完全清楚您想在这里做什么,但是如果 periods
中只有 "0"
,那么您可以将 属性 更改为:
public class Periods
{
[JsonProperty("0")]
public Period period { get; set; }
}
但我的猜测是您希望有 1 个以上的句点,因此您会有“0”、“1”、“2”...等等
没有整理一些代码来测试它,我不确定是否有好的方法。
您可能想尝试使 PeriodsModel
具有:
[JsonProperty("periods")]
public Dictionary<string, Period> periods { get; set; }
然后你根本不需要 Periods
class。
虽然通常当我有可变数量的东西要传递时,我只是使用 JSON 数组来代替,比如:
{
"id": "13",
"title": "party",
"periods": [
{
"label": "Period1",
"start_date": "2015-04-20",
"end_date": "2015-04-29"
}, {
"label": "Period2",
"start_date": "2015-04-20",
"end_date": "2015-04-29"
}, {
"label": "Period3",
"start_date": "2015-04-20",
"end_date": "2015-04-29"
}
]
}
我有以下 JSON 对象,我可以访问我的 Web API 控制器:
{
"id": "13",
"title": "party",
"periods": {
"0": {
"label": "Period",
"start_date": "2015-04-20",
"end_date": "2015-04-29"
}
}
}
我想尝试将其直接反序列化为我在 C# 中拥有的模型,但我也做不到。
这是我的模型:
public class PeriodsModel
{
[JsonProperty("id")]
public int id { get; set; }
[JsonProperty("title")]
public string title { get; set; }
[JsonProperty("periods")]
public Periods periods { get; set; }
}
public class Periods
{
[JsonProperty("0")]
public Dictionary<string,Period> period { get; set; }
}
public class Period
{
[JsonProperty("label")]
public string label { get; set; }
[JsonProperty("start_date")]
public string start_date { get; set; }
[JsonProperty("end_date")]
public string end_date { get; set; }
}
这是我在控制器中的方法:
public void Put([FromBody]JToken jsonbody)
{
var myJsonObject = JsonConvert.SerializeObject(jsonbody);
PeriodsModel model = JsonConvert.DeserializeObject<PeriodsModel>(myJsonObject);
}
这是我收到的错误消息:
An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code
Additional information: Error converting value "Period" to type 'CMS.WebApi.Controllers.ActivitiesController+Period'. Path 'periods.0.label', line 1, position 62.
您可以这样做,因为“0”更像是一个键而不是 属性 变量:
public class PeriodsModel
{
[JsonProperty("id")]
public int id { get; set; }
[JsonProperty("title")]
public string title { get; set; }
[JsonProperty("periods")]
public Dictionary<string, Period> periods { get; set; }
}
public class Period
{
[JsonProperty("label")]
public string label { get; set; }
[JsonProperty("start_date")]
public string start_date { get; set; }
[JsonProperty("end_date")]
public string end_date { get; set; }
}
错误信息是正确的。稍微深入研究数据...您有:
"0": {
"label": "Period",
"start_date": "2015-04-20",
"end_date": "2015-04-29"
}
你试着把它放进去:
public class Periods
{
[JsonProperty("0")]
public Dictionary<string,Period> period { get; set; }
}
public class Period
{
[JsonProperty("label")]
public string label { get; set; }
[JsonProperty("start_date")]
public string start_date { get; set; }
[JsonProperty("end_date")]
public string end_date { get; set; }
}
更具体地说,这个 JSON:
{
"label": "Period",
"start_date": "2015-04-20",
"end_date": "2015-04-29"
}
正在反序列化为 Dictionary<string,Period>
那么第一个属性:
"label": "Period"
需要反序列化为 <string, Period>
对。
左侧 "label"
正确转换为 string
但右侧是字符串 "Period"
,并且该字符串无法反序列化为 class 类型 Period
因此出现错误
Error converting value "Period" to type 'CMS.WebApi.Controllers.ActivitiesController+Period'
它正在尝试将字符串 "Period" 转换为 class 句点。
目前还不完全清楚您想在这里做什么,但是如果 periods
中只有 "0"
,那么您可以将 属性 更改为:
public class Periods
{
[JsonProperty("0")]
public Period period { get; set; }
}
但我的猜测是您希望有 1 个以上的句点,因此您会有“0”、“1”、“2”...等等
没有整理一些代码来测试它,我不确定是否有好的方法。
您可能想尝试使 PeriodsModel
具有:
[JsonProperty("periods")]
public Dictionary<string, Period> periods { get; set; }
然后你根本不需要 Periods
class。
虽然通常当我有可变数量的东西要传递时,我只是使用 JSON 数组来代替,比如:
{
"id": "13",
"title": "party",
"periods": [
{
"label": "Period1",
"start_date": "2015-04-20",
"end_date": "2015-04-29"
}, {
"label": "Period2",
"start_date": "2015-04-20",
"end_date": "2015-04-29"
}, {
"label": "Period3",
"start_date": "2015-04-20",
"end_date": "2015-04-29"
}
]
}