Search code examples
c#jsonjson-deserialization

Deserialize JSON message in run time


I'm trying to deseralizse a JSON message in run time and extract the values then assign to variables.

 dynamic Json = JsonConvert.DeserializeObject(result);

This is value deserialised in Json variable which I found in debugger

{{
  "geo": [
    {
      "x": 16324865.646724658,
      "y": -4967300.1743641077
    }
  ]
}}

I'm getting error when I tried to assign the parsed message into variables.

  double x1 = 0;
  double y1 = 0;

      
  x1 = Json.geo.x.Value();
  y1 = Json.geo.y.Value();

Error Message:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ''Newtonsoft.Json.Linq.JArray' does not contain a definition for 'x''

Solution

  •   double x1 = 0;
      double y1 = 0;
    
          
      x1 = Json.geo[0].x;
      y1 = Json.geo[0].y;
    

    Try the above once?