Hallo zusammen,
ich habe mal wieder ein JSON Problem zudem ich einfach keine Lösung finde.
json String:
{
"result": "ok",
"data": {
"time": "",
"address": {
"company": "",
"title": "",
"name": "",
"street": "",
"zip": "",
"town": "",
"country": "",
"phone": ""
},
"invoice": {
"company": "",
"title": "",
"name": "",
"street": "",
"zip": "",
"town": "",
"country": "",
"phone": ""
},
"comment": "",
"status": "0",
"dispatch": "0.00",
"delivery": {
"service": "0",
"tracking": ""
},
"coupon": {
"code": "",
"type": "",
"value": ""
},
"payment": {
"type": "",
"payed": "",
"fee": ""
},
"cart": [
{
"bestell_ID": "4444",
"artID": "5555",
"artnr": "TestNummer-001",
"artname": "Das ist mein Artikel Nr. 1",
"anzahl": "1",
"preis": "5.00",
"vat": "0.00"
},
{
"bestell_ID": "4444",
"artID": "6666",
"artnr": "TestNummer-002",
"artname": "Das ist mein Artikel Nr. 2",
"anzahl": "2",
"preis": "6.00",
"vat": "0.00"
}
],
"additional_orderfields": {
"Wie haben Sie uns gefunden?": "2"
}
}
}
Ich möchte die Daten aus "cart".
Ich verwende diese Zeile:
cart productData = JsonConvert.DeserializeObject<cart>(myJsonstring["data"]["cart"].ToString());
Und hier die Gets und Sets:
public class cart
{
public string bestell_ID { get; set; }
public string artID { get; set; }
public string artnr { get; set; }
public string artname { get; set; }
public string anzahl { get; set; }
public string preis { get; set; }
public string vat { get; set; }
}
Leider kommt es zur folgenden Exception:
Fehlermeldung:
"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'cart' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly."
Kann mir jemand einen Tipp geben wie ich mein Problem lösen kann?
Danke im Voraus
Erster Treffer bei der Googlesuche nach 1:1 Deiner Fehlermeldung sagt Dir die Lösung: Cannot deserialize the current JSON array (e.g. [1,2,3]). C#, cant figure the error out
=> Du versuchst ein Json Array auf eine einzelne Klasse zu casten.
Geht natürlich nicht.
Bitte Google in Zukunft selbst 😃
- performance is a feature -
Microsoft MVP - @Website - @AzureStuttgart - github.com/BenjaminAbt - Sustainable Code
Hallo CarnifeX,
Zusätzlich dazu gibts online sehr gute Converter wie http://json2csharp.com/
Gruss
Coffeebean
Microsoft MVP // Me // Blog // GitHub // @Egghead // All my talks // Speakerdeck