问题描述
我让Json返回如下:
I have Json returning as the following:
这些是我的班级...
These are my classes...
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 );
所以从这里我不确定如何访问列表中的纬度和经度项目。我可以访问像TrailName这样的东西没有问题,但不知道我在数组中的数组中的项目做错了什么。我想添加它们,以便我可以将它们绘制成类似于我所做的结构中的线条(硬编码值):
So from here I am not sure how I would access the Latitude and Longitude items in my list. I can access things like TrailName no problem but not sure what i'm doing wrong with the items in the array within the array. I would like to add them so that I can plot them as lines in a structure similar to this that I made (hardcoded values):
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);
推荐答案
我会解析而不是宣布很多课程json字符串如下
Instead of declaring a lot of classes, I would parse the json string as follows
JArray jArr = (JArray)JsonConvert.DeserializeObject(jsonstr);
foreach (var item in jArr)
{
foreach(var subitem in item["TrailCoordinates"])
{
Console.WriteLine(subitem["Longitude"] + " " + subitem["Latitude"]);
}
}
如果monotouch支持 dynamic
你也可以写
If monotouch supports dynamic
you can also write
dynamic jArr2 = JsonConvert.DeserializeObject(jsonstr);
foreach (dynamic item in jArr2)
{
foreach (var subitem in item.TrailCoordinates)
{
Console.WriteLine(subitem.Longitude + " " + subitem.Latitude);
}
}
你甚至可以使用Linq
You can even use Linq
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();
这篇关于使用Newtonsoft Json.NET解析多维JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!