C# 为什么JSon在子数组上给我空值?

C# 为什么JSon在子数组上给我空值?,c#,arrays,json,serialization,datacontracts,C#,Arrays,Json,Serialization,Datacontracts,我一直在努力让传入的Json消息进行序列化(并正确反序列化),这让我经历了一段可怕的时间。首先是传入的Json,我正在通过POSTMAN发布到我们的MVC rest服务。通过JsonLint对其进行了验证。主要问题是两个子数组accounts和propertyvalue都是null。serviceAddresses是profilePush的数组成员,其其他属性正在填充。在我将它们全部转换成数据契约之前,我得到的是serviceAddresses的空值。我错过了什么 ------------传入J

我一直在努力让传入的Json消息进行序列化(并正确反序列化),这让我经历了一段可怕的时间。首先是传入的Json,我正在通过POSTMAN发布到我们的MVC rest服务。通过JsonLint对其进行了验证。主要问题是两个子数组accounts和propertyvalue都是null。serviceAddresses是profilePush的数组成员,其其他属性正在填充。在我将它们全部转换成数据契约之前,我得到的是serviceAddresses的空值。我错过了什么

------------传入JSON------------------

 {
    "userId" : 15,
    "firstName" : "Michael",
    "lastName" : "Smith",
    "email" : "msmith@google.org",
    "deleted" : false,
    "serviceAddresses" : [ {
    "addressId" : 18,
    "address1" : "8401 Lorain Road",
    "address2" : "Suite 10A",
    "city" : "Newton Falls",
    "state" : "OH",
    "zip" : "44021",
    "deleted" : false,
    "accounts" : [],
    "propertyAttributes" : {
        "attr_name" : "heat_type",
        "attr_value" : "Gas",
        "attr_name" : "hot_water_heater_type",
        "attr_value" : "Gas",
        "attr_name" : "rent_own",
        "attr_value" : "Own",
        "attr_name" : "sq_ft",
        "attr_value" : "2000",
        "attr_name" : "stove_type",
        "attr_value" : "Gas"
                           }
                       }
     ]
   }





    [HttpPost]
    public JsonResult profileInformationPush(profilePush profile )
    {
          bool bError = false;

          string s = JsonConvert.SerializeObject(profile);

          profilePush deserializedProfile =
          JsonConvert.DeserializeObject<profilePush>(s);

    }

问题是这个JSON:

"propertyAttributes" : {
    "attr_name" : "heat_type",
    "attr_value" : "Gas",
    "attr_name" : "hot_water_heater_type",
    "attr_value" : "Gas",
    "attr_name" : "rent_own",
    "attr_value" : "Own",
    "attr_name" : "sq_ft",
    "attr_value" : "2000",
    "attr_name" : "stove_type",
    "attr_value" : "Gas"
}
以及您的结构:

    [DataMember(Name = "propertyAttributes", EmitDefaultValue = false)]
    public PropertyAttributes[] PropertyAttributes { get; set; }
它们不合适。根据您的JSON
propertyAttributes
是一个
对象
,而不是
数组
。由于json反序列化程序需要一个数组,但得到了一个对象,因此它无法填充您的属性,您将得到null

你确定这就是你得到的JSON吗?它甚至无效,因为属性名在一个对象中被多次使用。这是正确的:

"propertyAttributes": [
    {
        "attr_name": "heat_type",
        "attr_value": "Gas"
    }, {
        "attr_name": "hot_water_heater_type",
        "attr_value": "Gas"
    }
]

这里您得到一个数组
[…]
对象
{..}
每个对象都有一个属性
attr\u name
和一个属性
attr\u值

这是什么?它是Web服务还是其他类型的服务?验证它,但以静默方式拒绝所有重复的attr_name和attr_value字段,并仅保留最后一个字段。这不是真正有效的JSON。这就像做
x=1;x=2;x=3然后需要所有3个值。这是客户端发送的Json,它实际上通过JsonLint作为有效值传递。没有注意到缺少将propertyAttributes表示为数组的括号。将与客户端联系以修复其发送应用程序。谢谢
"propertyAttributes": [
    {
        "attr_name": "heat_type",
        "attr_value": "Gas"
    }, {
        "attr_name": "hot_water_heater_type",
        "attr_value": "Gas"
    }
]