JSON.NET和使用LINQ的数组

6

我看了这个 使用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中的数组,并且可以解释我可能犯的愚蠢错误或我未能看到的明显答案。

1个回答

10

看起来paths是一个点数组的数组,所以假设您想要每个路径的IEnumerable,您需要:

var allPaths = from p in jsonObject["features"].Children()["geometry"]
               select p["paths"].Children();

1
你做得很好 - 我在p["paths"]上缺少了Children(),所以我为了寻找真相搜索了4个小时,现在终于结束了。非常感谢你。 - Dylan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接