wuU wuU - 5 months ago 7
JSON Question

Deserialization of json string returns null values

how can I deserialize the ff json string:

{"stock":[{"name":"stock1","price":{"currency":"AUD","amount":103.50},"percent_change":-1.33,"volume":1583760,"symbol":"SC1"}],"as_of":"2016-06-10T15:20:00+08:00"}


I've tried the code:

JsonConvert.DeserializeObject<stock>(content);


where content variable is the json string above.
However I am getting null value of the properties.

Here are my classes:

public class price
{
public string currency { get; }

public double amount { get; }
}


public class stock
{
public string name { get; }

public price price { get; }

public double percent_change { get; }

public int volume { get; }

public string symbol { get; }
}


Thank you in advance!

Answer

Use this class for your json string-

public class Price
{
    public string currency { get; set; }
    public double amount { get; set; }
}

public class Stock
{
    public string name { get; set; }
    public Price price { get; set; }
    public double percent_change { get; set; }
    public int volume { get; set; }
    public string symbol { get; set; }
}

public class StockDetails
{
    public List<Stock> stock { get; set; }
    public string as_of { get; set; }
}
Comments