C#/ WP7 Json.net解析到列表

时间:2013-03-05 14:34:24

标签: c# json windows-phone-7

我在解析JSON以便在C#中列出时遇到问题。我正在使用Json.net来解析json。我的问题是我收到错误“错误转换值”高“键入”。请帮帮我。

我的json:

{"sold":{"high":40.64625,"low":35.02,"avg":37.929384985,"buy":40.28,"sell":40.3}}

我的代码:

public class sold
{
    public string high { get; set; }
    public string low { get; set; }
    public string avg { get; set; }
    public string buy { get; set; }
    public string sell { get; set; }

}

   void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        JObject something = JObject.Parse(e.Result);
        IList<JToken> results = something["sold"].Children().ToList();
        IList<sold> searchResults = new List<sold>();
        foreach (JToken result in results)
            {
sold searchResult = JsonConvert.DeserializeObject<sold>(result.ToString());
searchResults.Add(searchResult);
}

2 个答案:

答案 0 :(得分:0)

属性高是double的类型,您可以尝试使用此类的实现已售出

public class sold
{
public double high { get; set; }
public double low { get; set; }
public double avg { get; set; }
public double buy { get; set; }
public double sell { get; set; }
}

答案 1 :(得分:0)

尝试为已售商品数组添加包装类,并在一次调用中反序列化整个数组。我没有用json.net试过这个,但它适用于DataContractJsonSerializer。

public class SoldItemList
{
     public SoldItem[] sold { get;set; }
}
public class SoldItem
{
    public double high { get; set; }
    public double low { get; set; }
    public double avg { get; set; }
    public double buy { get; set; }
    public double sell { get; set; }
}