2016-06-11 80 views
0

我怎麼能反序列化FF JSON字符串:反序列化返回空值

{"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"} 

我已經試過代碼:

JsonConvert.DeserializeObject<stock>(content); 

在內容變量是上面的JSON字符串。 但是我得到屬性的空值。

這裏是我的課:

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

預先感謝您!

回答

1

使用這個類爲您JSON與字符串

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

非常感謝好友,它的工作! – wuU

2

添加setter:

public string name {get;組; }

- update -

您正在將庫存清單放入庫存。

添加類:

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

而且撥打:

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

你好,我這樣做是爲了所有的屬性,但我還是獲得空值。 – wuU