Deserialize JSON message in run time

40 Views Asked by At

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''
2

There are 2 best solutions below

1
Naruto On BEST ANSWER
  double x1 = 0;
  double y1 = 0;

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

Try the above once?

1
Richard On

This has fixed the issue.

double x1 = 0;
double y1 = 0;

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