I'm trying to deserialize a JSON object when I call a WebMethod. The deserialization successfully creates the array of object, but the values are "Nothing". What am I doing wrong?
My Object class:
public class Data
{
public Attribute[] Attributes{ get; set; }
}
public class Attribute
{
public string Category { get; set; }
public string Value { get; set; }
}
Here is my WebMethod:
<System.Web.Services.WebMethod()>
Public Shared Function A_Method(ByVal Attributes As Data)
'do things
End Function
And here is my JSON object that is being passed to the WebMethod:
{"Attributes":[
{
"Attribute":{
"Category":"Category1",
"Value":"Value1"
}
},
{
"Attribute":{
"Category":"Category2",
"Value":"Value2"
}
},
{
"Attribute":{
"Category":"Category3",
"Value":"Value3"
}
},
{
"Attribute":{
"Category":"Category4",
"Value":"Value4"
}
},
{
"Attribute":{
"Category":"Category5",
"Value":"Value5"
}
},
{
"Attribute":{
"Category":"Category6",
"Value":"Value6"
}
},
{
"Attribute":{
"Category":"Category7",
"Value":"Value7"
}
}
]
}
My problem is that I get the array of 7 Attributes with the Category and Value labels, but the values are "Nothing". What am I doing wrong?
Your object model does not match the shown JSON which actually maps to the following
public class Data {
public AttributeElement[] Attributes { get; set; }
}
public class AttributeElement {
public Attribute Attribute { get; set; }
}
public class Attribute {
public string Category { get; set; }
public string Value { get; set; }
}
Note how the elements in the array have an Attribute
property.
<System.Web.Services.WebMethod()>
Public Shared Function A_Method(ByVal data As Data)
'do things
Dim someAttribute = data.Attributes(0).Attribute
Dim category = someAttribute.Category
Dim value = someAttribute.Value
'...
End Function