2013-10-24 22 views
0

我一直有一個可怕的時間試圖讓一個傳入的Json消息序列化(和反序列化正確)。首先是我通過POSTMAN發佈到MVC休息服務的傳入Json。通過JsonLint進行驗證。主要問題是兩個子數組,account和propertyValues都是空的。 serviceAddresses是profilePush的一個數組成員,它的其他屬性正在填充。在我將它們全部納入DataContracts之前,我獲得了serviceAddresses的空值。我在這裏錯過了什麼?爲什麼我的JSon在子數組上給我空值?

------------傳入JSON ------------------

{ 
    "userId" : 15, 
    "firstName" : "Michael", 
    "lastName" : "Smith", 
    "email" : "[email protected]", 
    "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); 

    } 

------- - 這是什麼「個人資料」的模樣進來的程序--------------

{"UserId":15,"FirstName":"Michael","LastName":"Smith","Email":"[email protected]","Deleted":"False","ServiceAdresses":[{"AddressId":18,"Address1":"8401 Lorain Road","Address2":"Suite 10A","City":"Newton Falls","State":"OH","Zip":"44021","Deleted":"False","Accounts":null,"PropertyAttributes":null}]} 


--------------Data Contracts --------------------- 



    [DataContract] 
     public class accountInfo 
     { 
      [DataMember(Name="accountNumber", EmitDefaultValue = false)] 
      public string AccountNumber { get; set; } 
      [DataMember(Name="deleted", EmitDefaultValue = false)] 
      public string Deleted { get; set; } 
     } 



    [DataContract] 
    public class PropertyAttributes 
    { 
     [DataMember(Name="attr_name", EmitDefaultValue = false)] 
     public string Attr_Name { get; set; } 
     [DataMember(Name="attr_value", EmitDefaultValue = false)] 
     public string Attr_Value { get; set; } 

    } 

    [DataContract] 
    public class ServiceAddresses 
    { 
     [DataMember(Name="addressId", EmitDefaultValue = false)] 
     public int AddressId { get; set; } 
     [DataMember(Name="address1", EmitDefaultValue = false)] 
     public string Address1 { get; set; } 
     [DataMember(Name="address2", EmitDefaultValue= false)] 
     public string Address2 { get; set; } 
     [DataMember(Name="city", EmitDefaultValue = false)] 
     public string City { get; set; } 
     [DataMember(Name="state", EmitDefaultValue = false)] 
     public string State { get; set; } 
     [DataMember(Name="zip", EmitDefaultValue = false)] 
     public string Zip { get; set; } 
     [DataMember(Name="deleted", EmitDefaultValue = false)] 
     public string Deleted { get; set; } 
     [DataMember(Name="accounts", EmitDefaultValue = false)] 
     public accountInfo[] Accounts { get; set; } 

     [DataMember(Name = "propertyAttributes", EmitDefaultValue = false)] 
     public PropertyAttributes[] PropertyAttributes { get; set; } 
    } 

    [DataContract] 
    public class profilePush 
    { 
     [DataMember(Name="userId", EmitDefaultValue= false)] 
     public int UserId { get; set; } 
     [DataMember(Name="firstName", EmitDefaultValue = false)] 
     public string FirstName { get; set; } 
     [DataMember(Name="lastName", EmitDefaultValue = false)] 
     public string LastName { get; set; } 
     [DataMember(Name="email", EmitDefaultValue = false)] 
     public string Email { get; set; } 
     [DataMember(Name="deleted", EmitDefaultValue = false)] 
     public string Deleted { get; set; } 
     [DataMember(Name="serviceAddresses", EmitDefaultValue = false)] 
     public ServiceAddresses[] ServiceAddresses { get; set; } 
    } 
+0

這是什麼消耗?它是一種Web服務還是其他類型的服務? – Justin

+0

[JSONLint](http://jsonlint.com/)驗證它,但是默默拒絕所有重複的attr_name和attr_value字段,並且只保留最後一個。這不是真正有效的JSON。這就像'x = 1; x = 2; x = 3;'然後想要所有3個值。 –

回答

0

問題是這樣的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 propertyAttributesobject,而不是array。而且由於json反序列化器需要一個數組但是得到一個對象,它不能填充你的屬性,而你得到空值。

你確定這是你得到的JSON嗎?它甚至不是有效的,因爲屬性名稱在一個對象內多次使用。這將是正確的:

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

在這裏,你得到的每個對象{..}數組[...]與屬性attr_name和財產attr_value

+0

這是由客戶端發送的Json,它實際上通過了JsonLint的有效。沒有注意到缺少括號來將propertyAttributes表示爲數組。將聯繫客戶修復他們的發送應用程序。謝謝! –

相關問題