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

5
  • 1
    you can use JObject Commented Dec 2, 2019 at 8:35
  • I don't think it is possible without any application of sub string or parsing Commented Dec 2, 2019 at 8:36
  • @styx, JObject need parsing to work with payload. Commented Dec 2, 2019 at 8:36
  • 2
    @Nair he said without parsing into representative class, not without parsing at all Commented Dec 2, 2019 at 8: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. Commented Dec 2, 2019 at 8:42

1 Answer 1

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[][]>();
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.