简体   繁体   中英

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!

Add a setter:

public string name { get; set; }

-- update --

You are putting a list of stock into stock.

Add the class:

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

And call:

var result = JsonConvert.DeserializeObject<container>(content);

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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM