我看了这个 使用Json.net解析JSON 的问题和答案,它接近我所需要的。关键的区别是我需要解析一个由x,y对组成的数组,每条记录形成一条或多条线。这是我的输入示例:
{
"displayFieldName" : "FACILITYID",
"fieldAliases" : {
"FACILITYID" : "Facility Identifier",
},
"geometryType" : "esriGeometryPolyline",
"spatialReference" : {
"wkid" : 4326
},
"features" : [
{
"attributes" : {
"FACILITYID" : "",
"OBJECTID" : 1,
},
"geometry" :
{
"paths" :
[
[
[-80.3538239379999, 27.386884271],
[-80.3538100319999, 27.3868901900001],
[-80.3538157239999, 27.3869008510001]
]
]
}
},
{
"attributes" : {
"FACILITYID" : "",
"OBJECTID" : 2,
},
"geometry" :
{
"paths" :
[
[
[-80.3538239379999, 27.386884271],
[-80.3538295849999, 27.3868948420001]
]
]
}
}
]
}
(Check out http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/WaterTemplate/WaterDistributionNetwork/MapServer/9/query?outFields=*&where=OBJECTID%3C20&f=pjson 以获取完整列表)
我需要做的是将["features"]["geometry"]["paths"]数组解析为由x,y对组成的线。以下是获取所有路径(每个“记录”中的一个)的方法:
var allPaths = from p in jsonObject["features"].Children()["geometry"]
select p["paths"];
那给了我路径,然后我可以依次处理每个点阵列:
foreach (var eachPolylineInPath in allPaths)
{
IEnumerable<Point> linePoints = from line in eachPolylineInPath.Children()
select new Point(
(double) line[0],
(double) line[1],
double.NaN);
}
这就是我卡住的地方。我尝试使用来自JArray和LINQ的各种转换语句,但仍然得到空结果或异常,其中包括无法访问JProperty子值。
希望有人已经使用LINQ处理过JSON.NET中的数组,并且可以解释我可能犯的愚蠢错误或我未能看到的明显答案。