C# 使用 Json.NET 解析:“意外标记:StartObject”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12376474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:03:53  来源:igfitidea点击:

Parsing with Json.NET: "Unexpected token: StartObject"

c#jsonjson.net

提问by Yuki Kutsuya

I am parsing JSON and I get the following error:

我正在解析 JSON,但出现以下错误:

I am using the Newtonsoft.Json.NET dll.

我正在使用 Newtonsoft.Json.NET dll。

Error reading string. Unexpected token: StartObject. Path '[0]', line 1, position 2.

读取字符串时出错。意外标记:StartObject。路径“[0]”,第 1 行,位置 2。

This is the code that I have:

这是我拥有的代码:

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:

category 可以是以下字符串之一:

["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"]

[“管理工具”、“反悲伤工具”、“聊天相关”、“开发者工具”、“经济”、“修复”、“乐趣”、“常规”、“信息”、“机械”、“杂项” 、“角色扮演”、“传送”、“网站管理”、“世界编辑与管理”、“世界生成器”]

EDIT: This is the response I get:

编辑:这是我得到的回应:

 [{"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 :/.

有谁知道为什么它不起作用?它曾经在 :/ 之前工作过。

采纳答案by L.B

Your json is an array of complex object not an array of strings. Try this (TESTED):

您的 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;
}

EDIT

编辑

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