2013-03-05 30 views
0

我有解析JSON到C#列表的問題。我使用Json.net來解析json。我的問題是,我得到錯誤「錯誤轉換價值」高「鍵入」。請幫幫我。C#/ WP7 Json.net解析到列表

我的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); 
} 

回答

0

該房產爲雙類型,你可以用這個實現類的出售

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

現在我得到不同的錯誤「Error conversion value」high「to type'My_app.sold'。Path'',line 1,position 6.」你能幫忙嗎? – user1992794 2013-03-05 14:56:30

+0

Try:JsonConvert.DeserializeObject (something [「sold」]。ToString()); – 2013-03-05 15:17:17

+0

現在,它的工作。謝謝 – user1992794 2013-03-06 14:26:39

0
嘗試

嘗試爲銷售物品的數組添加包裝類並d在一次調用中串行化整個數組。我沒有試過這與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; } 
}