2012-10-30 89 views
1

我在标题提到,JSON是不完全反序列化,因为一些对象具有在控制器中的合适的值(I发送JSON来一个asp.net MVC 4控制器),但与性能出现问题存储在数组中的对象(准确的说,这个问题有数据显示)嵌套JSON反序列化不完全

我有以下类:没有正确的价值

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" 
} 

我在整理了这一点,因为我不理解或看到的问题是一个问题。

回答

1

免责声明:我是不是能精确地再现这一点,因为在我的情况Label和反序列化LabelId精细,虽然Type没有,所以我anwser可能不会解决你所遇到的问题。

在我的情况下,我只能将"$type":"labeled_arrow"属性反序列化,如果它不是第一个 - 如果它出现在"$labelid":"Label Name""$labeltext":"Label Name"之后,它可以正常工作。

奇怪的是,如果$type财产在JSON称为type和C#简称为[JsonProperty(PropertyName = "type")],它会反序列化精细,不分先后顺序的。

在你的情况下,你不能反序列化LabelLabelId要么,所以这可能是你的问题是由别的东西造成的。为了排除我建议,它可能会支付尝试修改一些属性名称的(可能是刚刚起飞的$),看看他们所造成的属性不正确地反序列化。

+0

我想过取出$,看它是否会引起任何问题,但什么都没有改变(我很高兴它没有,因为我真的需要它那里)。 我打算去看看它。谢谢。 –

+0

好的,我重新检查了这个问题,$实际上是问题所在。谢谢。 –

+0

没问题。乐于帮助。 –

1

我经历了同样的问题,但在我而言这是涉及到如何将数据正在从客户端发送。确保AJAX请求使用正确的标题和格式。例如:

 dataType: 'json', 
     contentType: 'application/json; charset=UTF-8', 
     data: JSON.stringify({ 
      MemberId : '123', 
      UserName: '456', 
      Parameters: [ 
       { Value : 'testing' }, 
       { Value : 'test2' } 
      ] 
     }),