Elisabeth
Elisabeth

Reputation: 21206

Can not access property of jobject

I fetch data from a geo location provider.

var data = JObject.Parse(json);

data is the below JObject.

{{
  "postalCodes": [
    {         
      "lng": 9.15,          
      "lat": 48.7
    }
  ]
}}

When I try data["postalCodes"] I get another object but I can not find "lat or "lng" properties.

Doing data["postalCodes"]["lat"] does not work.

How can I get lat properties value?

Upvotes: 0

Views: 1862

Answers (1)

Ferhat Sayan
Ferhat Sayan

Reputation: 226

Try with:

data["postalCodes"][0]["lat"]

or:

data["postalCodes"].First["lat"]

Because it is an array, so you have to say you want the first object in the array.

As Rhumborl mentioned, you should always validate your JSON. Have a look here: https://jsonformatter.curiousconcept.com/

{
  "postalCodes": [
    {         
      "lng": 9.15,          
      "lat": 48.7
    }
  ]
}

Upvotes: 2

Related Questions