2017-09-23 4 views
0

Ich möchte einige JSON-Strings wie folgt deserialisieren:JSON deserialize Subklassen

{"time":1506174868,"pairs":{ 
    "AAA":{"books":8,"min":0.1,"max":1.0,"fee":0.01}, 
    "AAX":{"books":8,"min":0.1,"max":1.0,"fee":0.01}, 
    "AQA":{"books":8,"min":0.1,"max":1.0,"fee":0.01} 
    }} 

wo AAA, AAX, ... gibt es Hunderte von Variationen

ich diesen Json als Klasse in VS2017 einfügen und nutzen Sie die folgende:

public class Rootobject 
{ 
    public int time { get; set; } 
    public Pairs pairs { get; set; } 
} 

public class Pairs 
{ 
    public AAA AAA { get; set; } 
    public AAX AAX { get; set; } 
    public AQA AQA { get; set; } 
} 

public class AAA 
{ 
    public int books { get; set; } 
    public float min { get; set; } 
    public float max { get; set; } 
    public float fee { get; set; } 
} 

public class AAX 
{ 
    public int books { get; set; } 
    public float min { get; set; } 
    public float max { get; set; } 
    public float fee { get; set; } 
} 

public class AQA 
{ 
    public int books { get; set; } 
    public float min { get; set; } 
    public float max { get; set; } 
    public float fee { get; set; } 
} 

ich würde versuchen Hunderte von Klassendeklarationen zu vermeiden, da alle Klassen gleich sind, außer ihre Namen.

Ich habe versucht, dies als Array oder Liste zu serialisieren, aber ich bekomme Ausnahme, da dies kein Array ist.

Ich benutze Newtonsoft JSON lib.

Danke

Antwort

1

Sicher, Sie das JSON-String analysieren können wie folgt zum Objekt:

für Ihren speziellen Fall
public class Rootobject 
{ 
    public int time { get; set; } 
    public Dictionary<string, ChildObject> pairs { get; set; } 
} 

public class ChildObject 
{ 

    public int books { get; set; } 
    public float min { get; set; } 
    public float max { get; set; } 
    public float fee { get; set; } 
} 

class Program 
{ 
    static string json = @" 
     {""time"":1506174868,""pairs"":{ 
     ""AAA"":{""books"":8,""min"":0.1,""max"":1.0,""fee"":0.01}, 
     ""AAX"":{""books"":8,""min"":0.1,""max"":1.0,""fee"":0.01}, 
     ""AQA"":{""books"":8,""min"":0.1,""max"":1.0,""fee"":0.01} 
     } 
    }"; 

    static void Main(string[] args) 
    { 
     Rootobject root = JsonConvert.DeserializeObject<Rootobject>(json); 
     foreach(var child in root.pairs) 
     { 
      Console.WriteLine(string.Format("Key: {0}, books:{1},min:{2},max:{3},fee:{4}", 
       child.Key, child.Value.books, child.Value.max, child.Value.min, child.Value.fee)); 
     } 

    } 
+0

Ja, es ist mein Problem gelöst. Vielen Dank – hkhk

+0

Gern geschehen :) – uyquoc

0

thisextendsthat ‚s Antwort ist in Ordnung. Es gibt jedoch voll dynamische Optionen für die Deserialisierung:

1) Parsen in ein JToken

var root = JObject.Parse(jsonString); 
var time = root["time"]; 

2) Parst in ein dynamic

dynamic d = JObject.Parse(jsonString); 
var time = d.time; 
+0

Ich habe versucht, die dynamische Version und das funktioniert auch, danke. Noch eine Frage: Wie bekomme ich "Bücher" von einem Kindobjekt? Der Schlüssel (AAA, AAX) kann jeder sein, ich weiß nicht welcher in der Antwort vorhanden ist – hkhk