I have a similar issue relate in this link Deserialize a JSON array in C#
But I can’t catch the array, so if someone can take a look and tell what I’m doing wrong, I would appreciate it. This is my JSON array:
{
"latitude": [
{
"ts": 1677055475800,
"value": "40.480946"
}
],
"longitude": [
{
"ts": 1677055475800,
"value": "-3.37441"
}
]
}
I tried the answer:
class Latitud
{
public Device latitude;
}
class Longitud
{
public Device longitude;
}
class Device
{
public string ts { get; set; }
public int value { get; set; }
}
JavaScriptSerializer ser = new JavaScriptSerializer();
var mylongitude= ser.Deserialize<List<Longitud>>(jsonData);
var mylatitude = ser.Deserialize<List<Latitud>>(jsonData);
What am I doing wrong?
2
Answers
Your structure does not match the source JSON.
You need a target object to represent the whole structure. Also, your
Device
class needs to match the structure of the inner data stored in the arrays:Lastly, I would recommend using
NewtonSoft
from theNewtonsoft.Json
package to deserialize it instead:JsonConvert
can be found in theNewtonsoft.Json
namespace. Make sure to add the package.When you deserialize the JSON string jsonData into the mylatitude and mylongitude variables, you are using the Deserialize<List>(jsonData) and Deserialize<List>(jsonData) methods, respectively.
However, the latitude and longitude properties in the JSON object are arrays, not objects. So you should deserialize them into a list of Coordinate objects rather than a list of Latitud or Longitud objects.
Can you please try this,