2016-03-21 8 views
1

Ich versuche dieses JSON zu deserialisieren, aber ich bekomme immer Fehler. Kann mir bitte jemand helfen? Wo habe ich einen Fehler gemacht?Wie dieses JSON mit JsonConvert.DeserializeObject deserialize

JSON: 

{ 
    "totalItems": 63, 
    "items": [ 
    { 
     "id": 100039812, 
     "group": { 
      "code": "DD", 
      "description": "Delivery Documents" 
     }, 
     "type": { 
      "code": "READ", 
      "description": "Logs" 
     }, 
     "reference": "ARLDR", 
     "date": "2015-03-24T00:00:00", 
     "description": "ALogs", 
     "notes": "", 
     "lastUpdateDate": "2015-03-24T14:06:42.063", 
     "location": "BOX A001", 
     "metadata": {} 
    }, 
    { 
     "id": 100039813, 
     "group": { 
      "code": "DD", 
      "description": "Delivery Documents" 
     }, 
     "type": { 
      "code": "BL", 
      "description": "Logbooks" 
     }, 
     "reference": "BALB", 
     "date": "2015-03-24T00:00:00", 
     "description": "Logbooks", 
     "notes": "", 
     "lastUpdateDate": "2015-03-24T14:07:42.44", 
     "location": "BOX A001", 
     "metadata": {} 
     } 
    ] 
} 

public class Documents 
{ 
    public int totalItems { get; set; } 
    public List<doc_items> items { get; set; } 
} 

public class doc_items 
{ 
    public int id { get; set; } 
    public List<group_items> group { get; set; } 
    public List<type_items> type { get; set; } 
    public string reference { get; set; } 
    public string date { get; set; } 
    public string description { get; set; } 
    public string notes { get; set; } 
    public string lastUpdateDate { get; set; } 
    public string location { get; set; } 
    public List<metadata_list> metadata { get; set; } 
} 

public class group_items 
{ 
    public string code { get; set; } 
    public string description { get; set; } 
} 

public class type_items 
{ 
    public string code { get; set; } 
    public string description { get; set; } 
} 

public class metadata_list 
{ 

} 

Dann ist dieses Ich nenne:

Documents myDocuments = JsonConvert.DeserializeObject<Documents>(responsetext.ToString()); 

und die folgende Fehlermeldung:

Error: Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type System.Collections.Generic.List`1[AerData.Ranorex.Web.Records.API_Documents+Documents]' because the type requires a JSON array (e.g. [...

+0

Fügen Sie Ihre JSON-Zeichenfolge in eine neue Klassendatei ein, indem Sie EDIT -> Inhalte einfügen -> JSON als Klassen einfügen oder http://jsonutils.com/ verwenden - Ihre Klassen stimmen nicht überein – Plutonix

Antwort

1

Im Gegenteil ‚Gruppe‘, ‚Typ‘ und ‚Metadaten‘ sind nicht Arrays so Ihre Klasse ändern zu:

public class doc_items 
{ 
    public int id { get; set; } 
    public group_items group { get; set; } 
    public type_items type { get; set; } 
    public string reference { get; set; } 
    public string date { get; set; } 
    public string description { get; set; } 
    public string notes { get; set; } 
    public string lastUpdateDate { get; set; } 
    public string location { get; set; } 
    public metadata_list metadata { get; set; } 
} 
0

aus der Definition der Documents Klasse gegeben, die group Eigenschaft eines doc_items muss sein eine Anordnung. In Ihrem gegebenen JSON ist es ein Objekt. Das gleiche gilt für die type Eigenschaft

0

Ich empfehle einen Konverter wie folgt aus: http://json2csharp.com/#

Versuchen Sie folgendes:

class Program 
{ 
    static void Main(string[] args) 
    { 
     var sr = new StreamReader("json.json"); 
     var jsonText = sr.ReadToEnd(); 
     Documents myDocuments = JsonConvert.DeserializeObject<Documents>(jsonText); 
    } 
} 

public class group_items 
{ 
    public string code { get; set; } 
    public string description { get; set; } 
} 

public class type_items 
{ 
    public string code { get; set; } 
    public string description { get; set; } 
} 

public class metadata_list 
{ 
} 

public class doc_items 
{ 
    public int id { get; set; } 
    public group_items GroupItems { get; set; } 
    public type_items TypeItems { get; set; } 
    public string reference { get; set; } 
    public string date { get; set; } 
    public string description { get; set; } 
    public string notes { get; set; } 
    public string lastUpdateDate { get; set; } 
    public string location { get; set; } 
    public metadata_list MetadataList { get; set; } 
} 

public class Documents 
{ 
    public int totalItems { get; set; } 
    public List<doc_items> items { get; set; } 
} 

Ihre Datendatei json.json

{ 
    "totalItems": 63, 
    "items": [ 
    { 
     "id": 100039812, 
     "group": { 
     "code": "DD", 
     "description": "Delivery Documents" 
     }, 
     "type": { 
     "code": "READ", 
     "description": "Logs" 
     }, 
     "reference": "ARLDR", 
     "date": "2015-03-24T00:00:00", 
     "description": "ALogs", 
    "notes": "", 
    "lastUpdateDate": "2015-03-24T14:06:42.063", 
    "location": "BOX A001", 
    "metadata": { } 
}, 
{ 
    "id": 100039813, 
    "group": { 
    "code": "DD", 
    "description": "Delivery Documents" 
    }, 
    "type": { 
    "code": "BL", 
    "description": "Logbooks" 
    }, 
    "reference": "BALB", 
    "date": "2015-03-24T00:00:00", 
    "description": "Logbooks", 
    "notes": "", 
    "lastUpdateDate": "2015-03-24T14:07:42.44", 
    "location": "BOX A001", 
    "metadata": { } 
    } 
    ] 
}