2012-04-02 29 views
3

Ich habe Json wie die folgende Rückkehr:Parsing Multidimensional JSON Array mit Newtonsoft Json.NET

[{ "CreatedBy": "GIS_DB", "CreatedDate": "2012.03.08 10.44: 00 AM "," Id ": 39," ModifiedBy ":" "," ModifiedDate ":" "," Name ":" CF-39 "," StatusId ": 1," TrailCoordinates ": [{" "CreatedBy" : "GIS_DB", "CreatedDate": "08.03.2012 10:44:00 AM", "Id": 1637, "Breitengrad": 32.76004207, "Längengrad": - 97.34006853, "ModifiedBy": "" , "ModifiedDate": "", "Sortierreihenfolge": 1, "TrailId": 39}, {"CreatedBy": "GIS_DB", "CreatedDate": "08.03.2012 10:44:00 AM", "Id": 1638, "Breitengrad": 32.76004333, "Längengrad": - 97.34012121, "ModifiedBy": "", "Modified Datum ":" "," Sortierreihenfolge ": 2," TrailId ": 39}]}, {" CreatedBy ":" GIS_DB "," CreatedDate ":" 08.03.2012 10:44:00 AM ", "Id": 40, "ModifiedBy": "", "ModifiedDate": "", "Name": "CF-40", "StatusId": 1, "TrailCoordinates": [{"CreatedBy": "GIS_DB", "CreatedDate": "3/8/2012 10:44:00 AM", "Id": 3755, "Breite": 32.76034332, "Länge": - 97.3402069, "ModifiedBy": "", "ModifiedDate": "", "SortOrder": 1, "TrailId": 40}, {"CreatedBy": "GIS_DB", "Erstelldatum": "08.03.2012 10:44:00 AM", "Id": 3756 "Latitude": 32.76019181, "Longitude": - 97.3402448 "ModifiedBy": "", "ModifiedDate": "", "SortOrder": 2 "TrailId": 40}]}]

Diese sind meine Klassen ...

public class Trails 
{ 
    [MonoTouch.Foundation.Preserve] 
    public Trails(){ TrailCoord = new List<trailcoords>();} 

    [MonoTouch.Foundation.Preserve, JsonProperty("Name")] 
    public string TrailName { get; set; } 

    [MonoTouch.Foundation.Preserve, JsonProperty("StatusId")] 
    public int StatusId { get; set; } 

    [MonoTouch.Foundation.Preserve, JsonProperty("TrailCoordinates")] 
    public List<trailcoords> TrailCoord { get; set; } 
// public trailcoords 

public Trails (string trailname, int statusid, List<trailcoords> trailcoord) 
{ 
    TrailName = trailname; 
     StatusId = statusid; 
     TrailCoord = trailcoord; 
    }  
} 

public class trailcoords 
{  
     [MonoTouch.Foundation.Preserve] 
    public trailcoords(){} 
[MonoTouch.Foundation.Preserve, JsonProperty("TrailId")] 
    public string TrailId { get; set; }  

    [MonoTouch.Foundation.Preserve, JsonProperty("Latitude")] 
    public double Latitude { get; set; }  
[MonoTouch.Foundation.Preserve, JsonProperty("Longitude")] 
    public double Longitude { get; set; } 

public trailcoords (string trailid, double latitude, double longitude) 
{ 
    TrailId = trailid; 
     Latitude = latitude; 
     Longitude = longitude; 
    } 
} 

List<Trails> Posts = JsonConvert.DeserializeObject<List<Trails>> (json); 

Also von hier bin ich mir nicht sicher, wie ich auf die Latitude und Longitude Elemente in meiner Liste zugreifen würde. Ich kann Dinge wie TrailName kein Problem, aber nicht sicher, was ich falsch mit den Elementen im Array innerhalb des Arrays tun. Ich möchte sie hinzufügen, so dass ich sie als Linien in einer Struktur, die ähnlich wie diese dargestellt werden können, die ich (hartcodierte Werte) gemacht:

CLLocationCoordinate2D[] tmpTrail1 = new CLLocationCoordinate2D[]{ 
    new CLLocationCoordinate2D(32.751531, -97.361755), 
    new CLLocationCoordinate2D(32.751451, -97.356625), 
    new CLLocationCoordinate2D(32.751500, -97.332077), 
}; 

CLLocationCoordinate2D[] tmpTrail2 = new CLLocationCoordinate2D[] { 
    new CLLocationCoordinate2D(32.727353, -97.361139), 
    new CLLocationCoordinate2D(32.747731, -97.359896), 
    new CLLocationCoordinate2D(32.765735, -97.360360), 
}; 

_trailOverlayPoints = new List<CLLocationCoordinate2D[]>(); 
_trailOverlayPoints.Add(tmpTrail1); 
_trailOverlayPoints.Add(tmpTrail2); 

Antwort

11

Statt viele Klassen zu erklären, würde ich die JSON-String parsen als folgt

JArray jArr = (JArray)JsonConvert.DeserializeObject(jsonstr); 
foreach (var item in jArr) 
{ 
    foreach(var subitem in item["TrailCoordinates"]) 
    { 
     Console.WriteLine(subitem["Longitude"] + " " + subitem["Latitude"]); 
    } 
} 

Wenn Monotouch unterstützt dynamic Sie auch

dynamic jArr2 = JsonConvert.DeserializeObject(jsonstr); 
foreach (dynamic item in jArr2) 
{ 
    foreach (var subitem in item.TrailCoordinates) 
    { 
     Console.WriteLine(subitem.Longitude + " " + subitem.Latitude); 
    } 
} 

Sie selbst verwenden können Linq

schreiben
JArray jArr = (JArray)JsonConvert.DeserializeObject(jsonstr); 
var coords = jArr 
      .Select(x => x["TrailCoordinates"]) 
      .SelectMany(x=>x) 
      .Where(x => x["TrailId"].ToString() == "40") 
      .Select(x => new { Lat = double.Parse(x["Latitude"].ToString()), Lon=double.Parse(x["Longitude"].ToString()) }) 
      .ToArray(); 
+0

Danke das ist großartig! Könnte ich mit Linq eine "where" -Klausel einfügen, so dass nur die Koordinaten für eine bestimmte Spur zurückgegeben werden? – k1komans

+0

@ user1288686 warum nicht? Ich habe die Antwort aktualisiert. –

+0

Große Antwort. Vielen Dank –