Home > OS >  Deserializing Json string c#
Deserializing Json string c#

Time:10-22

How can we deserialize this kind of json into a C# object? I need to read density and the coordinates.

{{
  "geometry": {
    "type": "Point",
    "coordinates": [
      51.5570726284386,
      25.39156280708102
    ]
  },
  "density": 1
}}

Thanks.

CodePudding user response:

your json is not valid. But if you remove an extra braket at the start and the end of a string

    json = json.Substring(1,json.Length-2);

then you can parse it

    using Newtonsoft.Json;

    var jsonObject = JObject.Parse(json);

    int density = (int)jsonObject["density"];
    double[] coordinates = jsonObject["geometry"]["coordinates"].ToObject<double[]>();

CodePudding user response:

Use pare object:

dynamic d = JObject.Parse(json);
var density  = d["density"];

  • Related