0

I want to Deserialise this json file in my Xamarin android App to only get the coordinates.

{
"type": "FeatureCollection",
"features": [
    {
        "type": "Feature",
        "id": "6849033",
        "geometry": {
            "type": "MultiPolygon",
            "coordinates": [
                [
                    [
                        [
                            6.562265,
                            40.36426
                        ],
                        [
                            6.5622743,
                            40.3642745
                        ],
                        [
                            6.5622944,
                            40.3642897
                        ],etc...

Here is my approach with my class

    public class Cadastre
{
    
    public List<List<List<List<float>>>> coordinates { get; set; }

    public Cadastre()
    {

    }

}

And finally here my code to Deserialize my json file

        string responseFinished = await GetJson();

        Cadastre c = JsonConvert.DeserializeObject<Cadastre>(responseFinished);

I tried many solutions but my coordinates are still null. If anyone has a solution or a lead I would be grateful.

2
  • 2
    You are missing a couple layers of outer container model public class Root public List<Feature> features { get; set; } } and public class Feature { public Cadastre geometry { get; set; } }. Copy your JSON to json2csharp.com or any of the other tools from How to auto-generate a C# class file from a JSON string and you should get the correct wrapper types. In fact I think your question could be closed as a duplicate of that, agree? Commented Dec 31, 2021 at 16:54
  • 1
    Thx your comment help me a lot now it's work ! Commented Dec 31, 2021 at 17:11

1 Answer 1

1

try this

var jsonObject=JObject.Parse(json);

var coordinates = ((JArray)jsonObject["features"][0]["geometry"]["coordinates"][0][0]).Select(c => new { One = c[0], Two = c[1]}).ToList();

result

[
  {
    "One": 6.562265,
    "Two": 40.36426
  },
  {
    "One": 6.5622743,
    "Two": 40.3642745
  },
  {
    "One": 6.5622944,
    "Two": 40.3642897
  }
]

or if you want to deserialize your way

List<List<List<List<float>>>> coordinates = ((JArray)jsonObject["features"][0]["geometry"]["coordinates"]).ToObject<List<List<List<List<float>>>>>();

result

[
  [
    [
      [
        6.562265,
        40.36426
      ],
      [
        6.5622745,
        40.364273
      ],
      [
        6.5622945,
        40.36429
      ]
    ]
  ]
]
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.