Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserializing json string with array of array with Json.NET

string json = @"{
'symbol':'XX',
'column_names":["Date","Open","High","Low","Close","Volume"],
'data':[
['2014-01-02',25.78,25.82,25.47,25.79,31843697.0],
['2013-12-31',25.81,26.04,25.77,25.96,22809682.0]]}";

public class DailyData
{
    public string symbol { get; set; }
    public List<OneDay> data { get; set; }
}

public class OneDay
{
     public DateTime date { get; set; }
     public double open { get; set; }
     public double high { get; set; }
     public double low { get; set; }
     public double close { get; set; }
     public double volume { get; set; }
}

DailyData dd = JsonConvert.DeserializeObject<DailyData>(json);

This is my json string and class I'm trying to deserialize it into with Json.net. It will work if I change public List<OneDay> data { get; set; } to public List<object> data { get; set; }. But in this case I have to do more steps further. Is there a neat solution to deserialize it in one go?

like image 533
Dork Avatar asked Jan 03 '14 12:01

Dork


People also ask

What is Jsonconvert DeserializeObject?

DeserializeObject(String, Type,JsonConverter[]) Deserializes the JSON to the specified . NET type using a collection of JsonConverter. DeserializeObject(String, Type, JsonSerializerSettings) Deserializes the JSON to the specified .

What is serialized and deserialized in JSON?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).


1 Answers

Yes, you have to use the JsonConverter class to tell the deserializer how to map an array of values to the OneDay class.

Example:

void Main()
{
    string json = @"{
                        'symbol':'XX',
                        'column_names':['Date','Open','High','Low','Close','Volume'],
                        'data':[
                            ['2014-01-02',25.78,25.82,25.47,25.79,31843697.0],
                            ['2013-12-31',25.81,26.04,25.77,25.96,22809682.0]
                                ]
                    }";

    DailyData dd = JsonConvert.DeserializeObject<DailyData>(json);
    dd.Dump();
}

class OneDayJsonConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        throw new NotImplementedException();
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        OneDay obj = new OneDay();
        obj.date = reader.ReadAsDateTime() ?? DateTime.MinValue;
        obj.open = (double)(reader.ReadAsDecimal() ?? 0);
        obj.high = (double)(reader.ReadAsDecimal()?? 0);
        obj.low = (double)(reader.ReadAsDecimal() ?? 0);
        obj.close = (double)(reader.ReadAsDecimal() ?? 0);
        obj.volume = (double)(reader.ReadAsDecimal() ?? 0);
        reader.Read(); 
        return obj;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

public class DailyData
{
    public string symbol { get; set; }
    public List<OneDay> data { get; set; }
}

[JsonConverter(typeof(OneDayJsonConverter))]
public class OneDay
{
    public DateTime date { get; set; }
    public double open { get; set; }
    public double high { get; set; }
    public double low { get; set; }
    public double close { get; set; }
    public double volume { get; set; }
}

Result:

enter image description here

like image 143
sloth Avatar answered Oct 05 '22 01:10

sloth