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.

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

1 Answers1

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
      ]
    ]
  ]
]
Serge
  • 40,935
  • 4
  • 18
  • 45