tags:

views:

639

answers:

2

I looked at this http://stackoverflow.com/questions/401756/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.

+4  A: 

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();
mancaus
You nailed it - I was missing the Children() on the p["paths"] so my 4 hour search for the truth is finished. Thank you so much.
Dylan
A: 

I don't have enough rep to upvote the answer, but Dylan, great question. I've been struggling with this exact issue for the past day. I'm also parsing JSON data from the ArcGIS Server REST API and trying to use JSON.NET as well. The documentation for JSON.NET provides few examples of how everything works together, so your example was extremely helpful to me. Thanks!

glad it helped. let me know if you need more working examples. got some code i can send your way. dvhthomas at gmail dot com
Dylan