I am parsing JSON and I get the following error:
I am using the Newtonsoft.Json.NET dll.
Error reading string. Unexpected token: StartObject. Path '[0]', line 1, position 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:
["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 :/.
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;
}
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With