Question

I looked at this Parsing JSON using Json.net question and answer and it's close to what I need. The critical difference Is that I need to parse an array of x,y pairs that form one or more lines per record. Here's an example of my input

{
"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 for the full listing)

What I need to do is parse the ["features"]["geometry"]["paths"] arrays in to lines composed of x,y pairs. Here is how I'm getting all of the paths (one per "record" as in the features array):

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

That gives me my paths, from which I can then process each point array in turn:

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

That's where I get stuck. I'm trying various casts from JArray and LINQ-y statements but keep getting null results or exceptions to the tune of JProperty child values cannot be accessed.

Hopefully someone has already dealt with converting arrays of arrays in JSON.NET using LINQ and can explain the stupid mistake I must be making, or the obvious answer I'm failing to see.

Was it helpful?

Solution

Looks like paths is an array of arrays of point, so assuming you want an IEnumerable for each path, you need:

var allPaths = from p in jsonObject["features"].Children()["geometry"]
               select p["paths"].Children();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top