Question

I've been having a ghastly time trying to get an incoming Json message to serialize (and deserialize correctly). First is the incoming Json that I'm posting to our MVC rest service via POSTMAN. Validated it through JsonLint. The main problem is the two sub-arrays, accounts and propertyValues come through as null. serviceAddresses is an array member of the profilePush and its other properties are populating. Before I made them all into DataContracts, I was getting null value for serviceAddresses. What am I missing here?

------------ Incoming 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);

    }

--------This is what "profile" looks like coming in to the procedure --------------

{"UserId":15,"FirstName":"Michael","LastName":"Smith","Email":"msmith@google.org","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; }
    }
Was it helpful?

Solution

The problem is this 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"
}

And your structure:

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

They do not fit. According to your JSON propertyAttributes is an object, not an array. And since the json deserializer expects an array but gets an object, it can't fill your property and you get null.

Are you sure this is the JSON you get? It's not even valid, because property names are used multiple times within one object. This would be correct:

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

Here you get an array [...] of objects {..} each with a property attr_name and a property attr_value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top