2

我在标题中提到,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; }    
}

我已经突出显示了没有正确值的字段。

空问题 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"
}

我在解决这个问题时遇到了问题,因为我不明白或看不出问题出在哪里。

4

2 回答 2

1

免责声明:我无法准确地重现这一点,就像我的情况一样Label,并且LabelId反序列化很好,虽然Type没有,所以我的回答器可能无法解决您遇到的问题。

"$type":"labeled_arrow"就我而言,如果它不是第一个属性,我只能反序列化它 - 如果它出现在它之后"$labelid":"Label Name"或者"$labeltext":"Label Name"它工作正常。

奇怪的是,如果在 Json$type中调用该属性并在 C# 中引用该属性,则无论顺序如何,它都会很好地反序列化。type[JsonProperty(PropertyName = "type")]

在您的情况下,您不能反序列化LabelLabelId两者之一,因此您的问题可能是由其他原因引起的。为了排除我的建议,尝试修改一些属性名称(可能只是去掉 $)以查看它们是否导致属性被错误地反序列化可能是值得的。

于 2012-10-31T04:55:43.437 回答
1

我遇到了同样的问题,但是在我的情况下,这与从客户端传输数据的方式有关。确保 AJAX 请求使用正确的标头和格式。例如:

        dataType: 'json',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify({
            MemberId : '123',
            UserName: '456',
            Parameters: [
                { Value : 'testing' },
                { Value : 'test2' }
            ]
        }),
于 2013-10-03T14:37:55.200 回答