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
  • 1
  • 10
  • 1
    you can use [JObject](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JObject.htm) – styx Dec 02 '19 at 08:35
  • I don't think it is possible without any application of sub string or parsing – S.N Dec 02 '19 at 08:36
  • @styx, JObject need parsing to work with payload. – S.N Dec 02 '19 at 08:36
  • 2
    @Nair he said without parsing into representative class, not without parsing at all – styx Dec 02 '19 at 08:37
  • @styx, If I read it correct, 'parsing it to a class that represents that JSON'. JObject.Parse initialize a new instance of the JObject class for the given json representation / data. – S.N Dec 02 '19 at 08:42

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
  • 559
  • 2
  • 11