0

I have a JSON similar to the following:

{
    "type": "FeatureCollection",
    "totalFeatures": "unknown",
    "features": [
        {
            "type": "Feature",
            "id": "xxx",
            "geometry": {
                "type": "MultiPolygon",
                "coordinates": [
                    [
                        570389.865,
                        4722149.567
                    ],
                    [
                        570389.865,
                        4722149.567
                    ]
                ]
            }
        }
    ]
}

Is there a way to get the coordinates property of the first feature without using substring or parsing it to an class that represents that JSON?

I'm looking for something standard for handling JSON strings as an Object, with methods for getting childs by name or similar.

Any help would be appreciated

teraxxx
  • 55
  • 1
  • 10

1 Answers1

0

You can use Newtonsoft.Json library.

Here's example of getting coordinates field (using JSONPath):

var parsed = JObject.Parse(yourJson);

// returns JToken (but actually it's JArray, derived from JToken)
var coordArrayToken = parsed.SelectToken("$.features[0].geometry.coordinates");
var coordinates = coordArrayToken.ToObject<decimal[][]>();

Of course you can use simple indexers:

var parsed = JObject.Parse(yourJson);

// returns JToken (but actually it's JArray, derived from JToken)
var coordArrayToken = parsed["features"].Children().First()["geometry"]["coordinates"];
var coordinates = coordArrayToken.ToObject<decimal[][]>();
Vyacheslav
  • 539
  • 2
  • 11