Deserialize array in Json

Asked

Viewed 433 times

1

I am accessing an API and the return is an array with latitude and longitude respectively. Unfortunately, I am not able to deserialize because it has no properties in Json.

How can I get this in C#?

Return:

[[-23.45317,-46.707126],[-23.453181,-46.707125],[-23.453196,-46.707123],
[-23.453484,-46.706913],[-23.453593,-46.706865],[-23.453759,-46.706795],
[-23.453942,-46.706744],[-23.45431,-46.7066],[-23.454667,-46.706528],
[-23.455409,-46.70635],[-23.45572,-46.706264],[-23.455805,-46.706178],
[-23.45586,-46.706151],[-23.455983,-46.706157],[-23.456166,-46.706137],
[-23.457182,-46.705768],[-23.457373,-46.705762],[-23.457346,-46.705728],
[-23.457339,-46.705664],[-23.457388,-46.705554],[-23.457451,-46.705489],
[-23.457565,-46.705387],[-23.457713,-46.705283],[-23.457778,-46.705215],
[-23.457809,-46.705126],[-23.457822,-46.705061],[-23.45783,-46.704993],
[-23.457832,-46.704927],[-23.457819,-46.704846],[-23.457761,-46.704654],
[-23.457719,-46.704532],[-23.457543,-46.704264],[-23.457378,-46.704054],
[-23.457296,-46.703964],[-23.457257,-46.703931]]
  • Is this a full JSON? I don’t think this is a valid JSON.

  • This JSON is valid, including I used it in my example.

2 answers

1


you can write your own Jsonconverter to handle this object, this way you will be able to map the array’s input in JSON to an Object and vice versa.

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class CoordenadaConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(Coordenada);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
            return null;
        var array = JArray.Load(reader);
        var coordenada = (existingValue as Coordenada ?? new Coordenada());
        coordenada.Latitude = (float)array[0];
        coordenada.Longitude = (float)array[1];
        return coordenada;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        var coordenada = (Coordenada)value;
        serializer.Serialize(writer, new float[] { coordenada.Latitude, coordenada.Longitude });
    }
}

[JsonConverter(typeof(CoordenadaConverter))]
public class Coordenada 
{
    public float Latitude {get; set;}
    public float Longitude {get; set;}
}

So you consume Json, you can do so:

var deserialized = JsonConvert.DeserializeObject<Coordenada[]>(json);       
foreach (var coordenada in deserialized) 
{
    Console.WriteLine("Latitude: " + coordenada.Latitude + ", Longitude: " + coordenada.Longitude); 
}       
var serialized = JsonConvert.SerializeObject(deserialized, Formatting.Indented);

you can see the above example running in the following Dotnetfiddle

If you prefer, as it is a very simple matrix, you can deserialize your JSON for a matrix float[][]:

var deserialized = JsonConvert.DeserializeObject<float[][]>(json);      
foreach (var coordenada in deserialized) 
{
    Console.WriteLine("Latitude: " + coordenada[0] + ", Longitude: " + coordenada[1]);  
}

var serialized = JsonConvert.SerializeObject(deserialized, Formatting.Indented);

0

You need to use Newtonsoft to do this.

Take a class to receive him;

public class Coordenada {
      public float latitude {get; set;}
      public float longitude {get; set;}
}

Understanding that Json is a javascript object, this is usually the format of a Json, and the conversion command:

string resultado = @"{
    latitude: '-23.45317'
    longitude: '-46.707126'
 }";

Coordenada CoordenadaUm = JsonConvert.DeserializeObject<Coordenada>(json); 

To finish, as you said yourself it is an array, you can use foreach and play all these values in a List<>.

Reference: http://www.newtonsoft.com/json/help/html/deserializeobject.htm

Browser other questions tagged

You are not signed in. Login or sign up in order to post.