使用LINQ的JSON.NET和数组

Dyl*_*lan 5 c# linq json.net

使用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]
      ]
    ]
  }
}
]
}
Run Code Online (Sandbox Code Playgroud)

(查看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"];
Run Code Online (Sandbox Code Playgroud)

这给了我我的路径,然后我可以依次处理每个点数组:

foreach (var eachPolylineInPath in allPaths)
{
  IEnumerable<Point> linePoints = from line in eachPolylineInPath.Children()
                                  select new Point(
                                                  (double) line[0],
                                                  (double) line[1],
                                                  double.NaN);
}
Run Code Online (Sandbox Code Playgroud)

这就是我被卡住的地方.我正在尝试从JArray和LINQ-y语句中进行各种强制转换,但是无法访问JProperty子值的调整中的null结果或异常.

希望有人已经使用LINQ处理在JSON.NET中转换数组的数组,并且可以解释我必须犯的愚蠢错误,或者我没有看到的明显答案.

man*_*aus 9

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

var allPaths = from p in jsonObject["features"].Children()["geometry"]
               select p["paths"].Children();
Run Code Online (Sandbox Code Playgroud)