Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

c#

json.net

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 :/.

like image 335
Yuki Kutsuya Avatar asked Sep 11 '12 19:09

Yuki Kutsuya


1 Answers

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);
}
like image 92
L.B Avatar answered Sep 23 '22 17:09

L.B