I am creating a Web Api method that should accept a list of objects via XML or JSON and add them to a database.
Here is a very basic version of what I currently have:
[HttpPost]
public HttpResponseMessage Put([FromBody]ProductAdd productAdd)
{
//do stuff with productadd object
return Request.CreateResponse(HttpStatusCode.OK);
}
The model structure of the list of objects it accepts is as follows:
public class ProductAdd
{
public List<ProductInformation> Products { get; set; }
}
public class ProductInformation
{
public string ProductName { get; set; }
}
The above works perfectly when I am using XML - (Content-Type: application/xml)
<?xml version="1.0" encoding="utf-8"?>
<ProductAdd>
<Products>
<ProductInformation>
<ProductName>Seahorse Necklace</ProductName>
</ProductInformation>
</Products>
<Products>
<ProductInformation>
<ProductName>Ping Pong Necklace</ProductName>
</ProductInformation>
</Products>
</ProductAdd>
But when I attempt to feed the same thing in using JSON (Content-Type: application/json), the Products list is empty
{
"ProductAdd": {
"Products": [
{
"ProductInformation": { "ProductName": "Seahorse Necklace" }
},
{
"ProductInformation": { "ProductName": "Ping Pong Necklace" }
}
]
}
}
Is there an issue with the JSON serializer when there is an array of objects within another object ?
Any ideas on what will fix this ?
Thanks
Edit: What serializers are you using for XML and Json? XML: XmlSerializer JSON: Newtonsoft
No comments:
Post a Comment