gpt4 book ai didi

c# - 使用 Json.NET : "Unexpected token: StartObject" 进行解析

转载 作者:太空狗 更新时间:2023-10-29 17:34:21 24 4
gpt4 key购买 nike

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

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

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

类别可以是以下字符串之一:

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

有人知道为什么它不起作用吗?它曾经在 :/.

之前工作过

最佳答案

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

关于c# - 使用 Json.NET : "Unexpected token: StartObject" 进行解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12376474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com