我在标题中提到json被反序列化不完整,因为一些对象在控制器中具有正确的值(我将json发送到asp.net mvc 4控制器),但问题出现在存储的对象的属性中数组(确切地说,问题出现在数据中)
我有以下课程:
public class Node
{
[JsonProperty("id")]
public int? Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
private Data _data = new Data();
[JsonProperty("data")]
public Data Data { get; set; }
private List<Adjacency> _adjacencies = new List<Adjacency>();
[JsonProperty("adjacencies")]
public List<Adjacency> Adjacencies
{
get { return _adjacencies; }
set { _adjacencies = value; }
}
private List<Instance> _dependendStates = new List<Instance>();
public List<Instance> DependentStates
{
get { return _dependendStates; }
set { _dependendStates = value; }
}
}
public class Data
{
[JsonProperty("$posX")]
public decimal PosX { get; set; }
[JsonProperty("$posY")]
public decimal PosY { get; set; }
}
public class Adjacency
{
[JsonProperty(PropertyName = "nodeTo")]
public string NodeTo { get; set; }
[JsonProperty(PropertyName = "data")]
public AdjData Data { get; set; }
//doesn't contain definition for automated transitions
}
public class AdjData
{
[JsonProperty(PropertyName = "$labelid")]
public string LabelId { get; set; }
[JsonProperty(PropertyName = "$labeltext")]
public string Label { get; set; }
[JsonProperty(PropertyName = "$type")]
public string Type { get; set; }
}
我突出显示了没有正确值的字段。
The null problem http://img19.imageshack.us/img19/530/36976913.png
json看起来像这样:
{
"result":[
{
"id":"100",
"name":"Start",
"data":{
"$posX":-100,
"$posY":-100,
"$deployed":true,
"$color":"#000000",
"$selected":"true"
},
"adjacencies":[
{
"nodeTo":"188",
"data":{
"$type":"labeled_arrow",
"$labelid":"Label Name",
"$labeltext":"Label Name"
}
}
]
},
{
"id":"188",
"name":"Second ",
"data":{
"$dim":20,
"$color":"#000000",
"$selected":"true"
},
"adjacencies":[
]
}
],
"smName":"gftrds"
}
我遇到问题,因为我不明白或查看问题所在。
答案 0 :(得分:1)
免责声明:我无法完全重现这一点,因为在我的情况Label
和LabelId
反序列化的罚款,但Type
没有,所以我的回答可能无法解决问题你有。
在我的情况下,我只能将"$type":"labeled_arrow"
属性反序列化,如果它不是第一个 - 如果它出现在"$labelid":"Label Name"
或"$labeltext":"Label Name"
后它运行正常。
奇怪的是,如果$type
属性在Json中被称为type
并在C#中被称为[JsonProperty(PropertyName = "type")]
,则无论顺序如何,它都会反序列化。
在您的情况下,您无法反序列化Label
或LabelId
,因此可能是您的问题是由其他原因造成的。为了排除我的建议,可能需要尝试修改一些属性名称(可能只是取下$)以查看它们是否导致属性被错误地反序列化。
答案 1 :(得分:1)
我遇到了同样的问题,但在我的情况下,这与数据从客户端传输的方式有关。确保AJAX请求使用正确的标头和格式。例如:
dataType: 'json',
contentType: 'application/json; charset=UTF-8',
data: JSON.stringify({
MemberId : '123',
UserName: '456',
Parameters: [
{ Value : 'testing' },
{ Value : 'test2' }
]
}),