我正在解析JSON,并得到以下错误:
我正在使用Newtonsoft.Json.NET dll。
读取字符串时出错。意外的标记:StartObject。路径"[0]",第1行,位置2。
这是我的代码:
public static List<string> GetPluginByCategory(string category)
{
var wc = new WebClient();
var json = wc.DownloadString("http://api.bukget.org/api2/bukkit/category/" + category);
var list = JsonConvert.DeserializeObject<List<string>>(json);
return list;
}
类别可以是以下字符串之一:
["管理工具","反Griefing工具","聊天相关","开发工具","经济","修复","有趣","通用","信息","机械","杂项","角色扮演","电传","网站管理","世界编辑和管理","世界生成器"]
编辑:这是我得到的回复:
[{"description": "Stop users swearingn", "name": "a5h73y", "plugname": "NoSwear"}, {"description": "Be sure that your server rules are read and accepted!", "name": "acceptdarules", "plugname": "AcceptDaRules"}]
有人知道为什么它不起作用吗?它以前工作过:/。
json是一个复杂对象数组,而不是字符串数组。试试这个(测试):
WebClient wc = new WebClient();
string json = wc.DownloadString("http://api.bukget.org/api2/bukkit/category/Teleportation");
var items = JsonConvert.DeserializeObject<List<MyItem>>(json);
public class MyItem
{
public string description;
public string name;
public string plugname;
}
编辑
WebClient wc = new WebClient();
var json = wc.DownloadString("http://api.bukget.org/api2/bukkit/plugin/aboot");
dynamic dynObj = JsonConvert.DeserializeObject(json);
Console.WriteLine("{0} {1}", dynObj.plugname,dynObj.link);
foreach (var version in dynObj.versions)
{
var dt = new DateTime(1970, 1, 1).AddSeconds((int)version.date);
Console.WriteLine("t{0} {1} {2}",version.version, version.download, dt);
}