使用Json.NET进行解析:“意外令牌:StartObject" [英] Parsing with Json.NET: "Unexpected token: StartObject"
问题描述
我正在解析JSON并收到以下错误:
I am parsing JSON and I get the following error:
我正在使用Newtonsoft.Json.NET dll.
I am using the Newtonsoft.Json.NET dll.
读取字符串时出错.意外的令牌:StartObject.路径"[0]",第1行,位置2.
Error reading string. Unexpected token: StartObject. Path '[0]', line 1, position 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;
}
类别可以是以下字符串之一:
category can be one of the following strings:
[管理工具",反悲伤工具",聊天相关",开发者工具",经济",修复",娱乐",常规",信息性",机械性", 其他",角色扮演",传送",网站管理",世界编辑和管理",世界生成器"]
["Admin Tools", "Anti-Griefing Tools", "Chat Related", "Developer Tools", "Economy", "Fixes", "Fun", "General", "Informational", "Mechanics", "Miscellaneous", "Role Playing", "Teleportation", "Website Administration", "World Editing and Management", "World Generators"]
这是我得到的答复:
[{"description": "Stop users swearing\n", "name": "a5h73y", "plugname": "NoSwear"}, {"description": "Be sure that your server rules are read and accepted!", "name": "acceptdarules", "plugname": "AcceptDaRules"}]
有人知道为什么它不起作用吗?它曾经在:/.
Does anybody know why it doesn't work? It used to work before :/.
推荐答案
您的json是复杂对象的数组,而不是字符串的数组.试试这个(测试):
Your json is an array of complex object not an array of strings. Try this (TESTED):
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);
}
这篇关于使用Json.NET进行解析:“意外令牌:StartObject"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!