我有一个问题,我似乎无法找到关于如何解决它的谷歌线索。我试图解析一个RESTful-api服务的响应。试图解析一个RESTful api响应
[{"account_id":"5401585","history":"84967869|2|03\/30\/2012,84972342|2|03\/30\/2012,85312563|2|04\/02\/2 012,85314831|2|04\/02\/2012,85318847|2|04\/02\/2012,85435388|2|04\/03\/2012,100244102|2|09\/09\/2012,100 245865|2|09\/09\/2012,100249440|2|09\/09\/2012,100251434|2|09\/09\/2012"}]'
我不明白我该如何将这个响应放到列表中,就像我的模型所说的那样。
基本上,我想要把每一行除以“,”加到列表中。
我的代码看起来是这样的:
public class MatchHistoryParser
{
public RootObject get()
{
using (var webClient = new System.Net.WebClient())
{
string URL2 = @"api url";
var json = webClient.DownloadString(URL2);
RootObject match = JsonConvert.DeserializeObject<RootObject>(json);
return match;
}
}
}
public class RootObject
{
public string account_id { get; set; }
public string history { get; set; }
public string win_loss_history { get; set; }
}
抛出错误:” 其他信息:无法当前JSON阵列(例如[1,2,3])反序列化为类型 'HoNEnemy.BL.RootObject'因为该类型需要JSON对象(例如{“name”:“value”})才能正确反序列化。“
你需要一个像JSON.NET http://james.newtonking.com/json或Microsoft内置的JSON解析器。或者从JSON中生成C#,以了解JSON是什么:http://json2csharp.com/ – MatthewMartin 2014-10-08 14:38:06
该字符串的内容对JavaScript不透明。一旦你在一个字符串中,这是一个特别的格式。 (这意味着历史) – MatthewMartin 2014-10-08 14:39:32
历史应该是一个字符串。 public class RootObject { public string account_id {get;组; } public string history {get;组; } } – MatthewMartin 2014-10-08 14:40:12