-2

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?

JuanR
  • 7,405
  • 1
  • 19
  • 30
Aismel
  • 3
  • 2

2 Answers2

1

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:

//This represents your main structure
public class SomeTargetObject
{
    public Device[] Latitude { get; set; }

    public Device[] Longitude { get; set; }
}

//This represents the inner data 
public class Device
{
    public string ts { get; set;}
    public string value { get; set; }
}

Lastly, I would recommend using NewtonSoft from the Newtonsoft.Json package to deserialize it instead:

var json = @"{
                ""latitude"": [
                    {
                        ""ts"": 1677055475800,
                        ""value"": ""40.480946""
                    }
                ],
                ""longitude"": [
                    {
                        ""ts"": 1677055475800,
                        ""value"": ""-3.37441""
                    }
                ]
            }";

var obj = JsonConvert.DeserializeObject<SomeTargetObject>(json);

JsonConvert can be found in the Newtonsoft.Json namespace. Make sure to add the package.

JuanR
  • 7,405
  • 1
  • 19
  • 30
0

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,

public class Location
{
     public List<Coordinate> latitude { get; set; }
     public List<Coordinate> longitude { get; set; }
}

public class Coordinate
{
    public long ts { get; set; }
    public string value { get; set; }
}

// Deserialize the JSON string into an object
string jsonString = "Your JSON code";

JavaScriptSerializer serializer = new JavaScriptSerializer();
Location location = serializer.Deserialize<Location>(jsonString);


Console.WriteLine("Latitude: " + location.latitude[0].value);
Console.WriteLine("Longitude: " + location.longitude[0].value);