JSON-Array von Objekten, um Modell in WebAPI mit FromBody

Erstelle ich ein Web-Api-Methode, die annehmen sollte, eine Liste von Objekten über XML-oder JSON und fügen Sie Sie in einer Datenbank.

Hier ist eine sehr einfache version von dem, was ich derzeit habe:

[HttpPost]
public HttpResponseMessage Put([FromBody]ProductAdd productAdd)
{
    //do stuff with productadd object
    return Request.CreateResponse(HttpStatusCode.OK);
}

Modell die Struktur der Liste der Objekte, die es akzeptiert, ist wie folgt:

public class ProductAdd
{
    public List<ProductInformation> Products { get; set; }
}

public class ProductInformation
{
    public string ProductName { get; set; }
}

Den oben funktioniert perfekt, wenn ich mit 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>

JSON-Array von Objekten, um Modell in WebAPI mit FromBody

Aber wenn ich versuchen zu füttern die gleiche Sache in der Verwendung von JSON (Content-Type: application/json), die Liste der Produkte ist leer

{
  "ProductAdd": {
    "Products": [
      {
        "ProductInformation": { "ProductName": "Seahorse Necklace" }
      },
      {
        "ProductInformation": { "ProductName": "Ping Pong Necklace" }
      }
    ]
  }
}

JSON-Array von Objekten, um Modell in WebAPI mit FromBody

Ist es ein Problem mit dem JSON-serializer, wenn es ein array von Objekten in ein anderes Objekt ?

Irgendwelche Ideen an was dieses Problem beheben ?

Dank

Bearbeiten:
Was serialisierungsprogramme verwenden Sie für XML-und Json?
XML: XmlSerializer
JSON: Newtonsoft

InformationsquelleAutor strvanica | 2014-11-21
Schreibe einen Kommentar