2016-08-05 18 views
5

Otrzymuję JSON z powrotem z API, które wygląda tak:Cofnięcie zagnieżdżone JSON w C# obiekty

{ 
    "Items": { 
    "Item322A": [{ 
     "prop1": "string", 
     "prop2": "string", 
     "prop3": 1, 
     "prop4": false 
    },{ 
     "prop1": "string", 
     "prop2": "string", 
     "prop3": 0, 
     "prop4": false 
    }], 
     "Item2B": [{ 
     "prop1": "string", 
     "prop2": "string", 
     "prop3": 14, 
     "prop4": true 
    }] 
    }, 
    "Errors": ["String"] 
} 

Próbowałem kilka podejść do reprezentowania tego JSON w C# obiektów (zbyt wiele do listy tutaj). Próbowałem z list i słowników, tutaj jest ostatnim przykładem, jak starałem się ją reprezentować:

private class Response 
    { 
     public Item Items { get; set; } 
     public string[] Errors { get; set; } 
    } 

    private class Item 
    { 
     public List<SubItem> SubItems { get; set; } 
    } 

    private class SubItem 
    { 
     public List<Info> Infos { get; set; } 
    } 

    private class Info 
    { 
     public string Prop1 { get; set; } 
     public string Prop2 { get; set; } 
     public int Prop3 { get; set; } 
     public bool Prop4 { get; set; } 
    } 

A oto metoda używam deserializacji JSON:

using (var sr = new StringReader(responseJSON)) 
    using (var jr = new JsonTextReader(sr)) 
    { 
     var serial = new JsonSerializer(); 
     serial.Formatting = Formatting.Indented; 
     var obj = serial.Deserialize<Response>(jr); 
    } 

obj zawiera Items i Errors. I Items zawiera SubItems, ale SubItems jest null. Tak więc nic poza wyjątkiem Errors jest faktycznie uzyskiwanie deserialized.

Powinno być proste, ale z jakiegoś powodu nie mogę dowiedzieć się prawidłową reprezentację obiektu

Odpowiedz

8

Dla "Items" użyć Dictionary<string, List<Info>>, tj:

class Response 
{ 
    public Dictionary<string, List<Info>> Items { get; set; } 
    public string[] Errors { get; set; } 
} 

class Info 
{ 
    public string Prop1 { get; set; } 
    public string Prop2 { get; set; } 
    public int Prop3 { get; set; } 
    public bool Prop4 { get; set; } 
} 

ta zakłada, że ​​nazwy pozycji "Item322A" i "Item2B" różnią się od odpowiedzi na odpowiedzi i odczytuje te nazwy jako klucze słownika.

Próbka fiddle.

+0

Fajnie, mam zamiar spróbować. Po prostu próbowałem podobnego podejścia, ale słownik znalazł się na liście! – user3574076

+0

To działało. Okazuje się, że moja ostatnia próba była * tak blisko * – user3574076

10

Używaj to ta strona reprezentacji:

http://json2csharp.com/

coś takiego może pomóc

public class Item322A 
{ 
    public string prop1 { get; set; } 
    public string prop2 { get; set; } 
    public int prop3 { get; set; } 
    public bool prop4 { get; set; } 
} 

public class Item2B 
{ 
    public string prop1 { get; set; } 
    public string prop2 { get; set; } 
    public int prop3 { get; set; } 
    public bool prop4 { get; set; } 
} 

public class Items 
{ 
    public List<Item322A> Item322A { get; set; } 
    public List<Item2B> Item2B { get; set; } 
} 

public class jsonObject 
{ 
    public Items Items { get; set; } 
    public List<string> Errors { get; set; } 
} 

Oto jak deserializowania (użyj JsonConvert klasa):

jsonObject ourlisting = JsonConvert.DeserializeObject<jsonObject>(strJSON); 
+0

Item322A i Item2B nie są stałymi. Ale dzięki za odpowiedź – user3574076

Powiązane problemy