Deserialize array in Json

1

I'm accessing an API and the return is an array with latitude and longitude respectively. Unfortunately, I can not deserialize because it does not have properties in Json.

How can I do to achieve 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]]
    
asked by anonymous 27.04.2016 / 09:13

2 answers

1

You can write your own JsonConverter to handle this object, so you will be able to map the array index 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 can consume Json, you can do it like this:

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 example above working in the following DotNetFiddle

If you prefer, since this is a very simple array, you can deserialize your JSON to a float[][] array:

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);
    
27.04.2016 / 14:33
0

You need to use Newtonsoft to do this.

Make a class to receive it;

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 yourself said it is an array, you can use foreach and throw all those values into a List < >.

Reference: link

    
27.04.2016 / 09:40