2016-07-12 10 views
0

Ich habe folgendes json:JSON.net Zugriff Werte von entserialisierten Liste mehrere Typen enthalten, einschließlich Objekte

var x = [ 
    [99,"abc","2dp",{"Group": 0,"Total":[4, 1]}], 
    [7,"x","date"], 
    [60,"x","1dp",{"Group": 1}], 
    ... 
] 

Ich brauche diese json deserialisieren, aber ich habe Probleme mit dem Objekt in Feld 3. Diese war ein erster Versuch:

string x = "[[99,\"abc\",\"2dp\",{\"Group\": 0,\"Total\":[4, 1]}],[7,\"x\",\"date\"],[60,\"x\",\"1dp\",{\"Group\": 1}]]"; 
List<List<object>> xobj = JsonConvert.DeserializeObject<List<List<object>>>(x); 

Dies scheint zu funktionieren. Mit Hilfe der Zwischenfenster in Visual Studio 2015:

xobj[0][0]; 
99 

xobj[1][2]; 
"date" 

aber ich bin nicht sicher, wie das Objekt im Feld für den Zugriff auf 3?

xobj[0][3]; 
{{ 
    "Group": 0, 
    "Total": [ 
    4, 
    1 
    ] 
}} 
    ChildrenTokens: Count = 2 
    Count: 2 
    First: {"Group": 0} 
    HasValues: true 
    Last: {"Total": [ 
    4, 
    1 
]} 
    Next: null 
    Parent: null 
    Path: "" 
    Previous: null 
    Root: {{ 
    "Group": 0, 
    "Total": [ 
    4, 
    1 
    ] 
}} 
    Type: Object 
    Results View: Expanding the Results View will enumerate the IEnumerable 
    Dynamic View: Expanding the Dynamic View will get the dynamic members for the object 

xobj[0][3].Root["Group"]; 
error CS1061: 'object' does not contain a definition for 'Root' and no extension method 'Root' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) 

Ich bemerkte es eine ‚erste‘ Methode hatte, also versuchte ich das, aber auch kein Glück:

xobj[0][3][0]; 
error CS0021: Cannot apply indexing with [] to an expression of type 'object' 

Wie kann ich auf Werte innerhalb des Objekts jeder Liste?

Antwort

1

Wenn Sie wollen einfach nur den Zugang der optionale Eintrag diese dann ausprobieren:

static void Main(string[] args) 
    { 
     string x = "[[99,\"abc\",\"2dp\",{\"Group\": 0,\"Total\":[4, 1]}],[7,\"x\",\"date\"],[60,\"x\",\"1dp\",{\"Group\": 1}]]"; 
     List<List<object>> xobj = JsonConvert.DeserializeObject<List<List<object>>>(x); 

     for (int i = 0; i < xobj.Count; i++) 
     { 
      // Do something with index 0 to 3 
      if (xobj[i].Count == 4) 
      { 
       // I have the optional entry with Group & Total properties 
       dynamic opt = xobj[i][3]; 
       Console.WriteLine("GROUP: " + opt.Group); // Mandatory 
       Console.WriteLine("GROUP value: " + opt.Group.Value); 

       if (opt["Total"] != null) 
       { 
        Console.WriteLine("TOTAL: " + opt.Total); 
        Console.WriteLine("TOTAL item 0 value: " + opt.Total[0].Value); 
        Console.WriteLine("TOTAL item 1 value: " + opt.Total[1].Value); 
       } 
      } 
     } 

     Console.ReadLine(); 
    } 
+0

diese gibt dabei nur eine ganze Menge Text, und nicht ein 'int' Wert wie ich wollte: {1} Erstens: '((Newtonsoft.Json.Linq.JToken) opt.Group) .Erste' hat eine Ausnahme vom Typ 'System.InvalidOperationException' ausgelöst HasValues: false Last: '((Newtonsoft.Json.Linq.JToken) opt.Group) .Last 'hat eine Ausnahme vom Typ' System.InvalidOperationException 'ausgelöst Weiter: null Parent: {"Group": 1} Pfad: "Group" Zurück: null Root: {{ "Gruppe": 1 – mulllhausen

+0

können Sie mir sagen, wie man den Wert der Gruppe jedoch erhält? Wie bekomme ich 'int groupVal = opt.Group ...?' – mulllhausen

+0

ahh es mit 'opt.Group.Value' – mulllhausen

Verwandte Themen