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.
LINQ to JSON is an API for working with JSON objects. It has been designed with LINQ in mind to enable quick querying and creation of JSON objects. LINQ to JSON sits under the Newtonsoft. Json.
The JToken hierarchy looks like this: JToken - abstract base class JContainer - abstract base class of JTokens that can contain other JTokens JArray - represents a JSON array (contains an ordered list of JTokens) JObject - represents a JSON object (contains a collection of JProperties) JProperty - represents a JSON ...
Deserializes the JSON to the specified . NET type. Deserializes the JSON to the specified . NET type using a collection of JsonConverter.
LINQ (Language Integrated Query) is uniform query syntax in C# and VB.NET to retrieve data from different sources and formats. It is integrated in C# or VB, thereby eliminating the mismatch between programming languages and databases, as well as providing a single querying interface for different types of data sources.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With