這裏是我的JSON數據無法反序列化JSON對象
[
{
"market_id": "21",
"coin": "DarkCoin",
"code": "DRK",
"exchange": "BTC",
"last_price": "0.01777975",
"yesterday_price": "0.01770278",
"change": "+0.43",
"24hhigh": "0.01800280",
"24hlow": "0.01752015",
"24hvol": "404.202",
"top_bid": "0.01777975",
"top_ask": "0.01790000"
}
]
這裏是我的類
public class Model_MarketStats
{
[JsonProperty(PropertyName="market_id")]
public string market_id { get; set; }
[JsonProperty(PropertyName = "code")]
public string code { get; set; }
[JsonProperty(PropertyName = "exchange")]
public string exchange { get; set; }
[JsonProperty(PropertyName = "last_price")]
public string last_price { get; set; }
[JsonProperty(PropertyName = "yesterday_price")]
public string yesterday_price { get; set; }
[JsonProperty(PropertyName = "change")]
public string change { get; set; }
[JsonProperty(PropertyName = "24hhigh")]
public string highest { get; set; }
[JsonProperty(PropertyName = "24hlow")]
public string lowest { get; set; }
[JsonProperty(PropertyName = "24hvol")]
public string volume { get; set; }
[JsonProperty(PropertyName = "top_bid")]
public string top_bid { get; set; }
[JsonProperty(PropertyName = "top_ask")]
public string top_ask { get; set; }
}
錯誤說
Newtonsoft.Json.JsonSerializationException:無法反序列化當前的JSON數組(例如[1,2,3])類型爲'MintpalAPI.Model_MarketStats_Root',因爲該類型需要一個JSON對象(例如{「name」:「value」})來釋放正確對齊。 要解決此錯誤,請將JSON更改爲JSON對象(例如{「name」:「value」})或將反序列化類型更改爲實現集合接口(例如ICollection,IList)的數組或類型,如List可以從JSON數組中反序列化。 JsonArrayAttribute也可以添加到類型中,以強制它從JSON數組反序列化。
這是我desialize的JSON
Model_MarketStats = JsonConvert.DeserializeObject<Model_MarketStats>(json);
是JSON('[{...}]')是一個列表。你的代碼(如OP已經嘗試過)不能工作 – EZI