2016-06-11 79 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