我使用 Json.net问答查看了这个Parsing 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]
]
]
}
}
]
}
(查看http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/WaterTemplate/WaterDistributionNetwork/MapServer/9/query?outFields= *&where=OBJECTID%3C20&f=pjson 以获取完整列表)
我需要做的是将 ["features"]["geometry"]["paths"] 数组解析为由 x,y 对组成的行。这是我获取所有路径的方式(每个“记录”一个,如 features 数组):
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-y 语句中进行各种强制转换,但由于无法访问 JProperty 子值,我不断得到空结果或异常。
希望有人已经处理过使用 LINQ 在 JSON.NET 中转换数组的数组,并且可以解释我必须犯的愚蠢错误,或者我没有看到的明显答案。