14

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

8
  • Please post the exact JSON data received from the server. Chances are it is not "an array of strings" for whatever reason. Commented Sep 11, 2012 at 19:26
  • 1
    (Actually, seeing the response is long, post a short but should-be-valid-per-expectations version of the response that shows the same error. The error is that JSON.NET is finding a { where it expects a string start. List<string> maps to ["a", "b", "etc"] whereas the data is [{someobj1}, {someobj2}, {etc}].) Commented Sep 11, 2012 at 19:33
  • Here's a small piece of code 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"} Commented Sep 11, 2012 at 19:38
  • Update that in the main post (don't forget to add a ] at the end to make it a representative sample). Commented Sep 11, 2012 at 19:39
  • When it "used to work", was the data from the server the same? Maybe it changed? Commented Sep 11, 2012 at 19:40

1 Answer 1

23

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);
}
Sign up to request clarification or add additional context in comments.

20 Comments

The "used to work" in the post has been throwing me off; is there a way to get Json.NET to "stuff" complex values into strings (as JSON)? Perhaps a converter?
@pst you can use List<object> type to deserialize and call a ToString() on every item in the list. This would return something like { "description": null, "name": "abitofrealism", "plugname": "AbitOfRealism" }
Yeah, that's what I thought .. anyway, issue seems to be "API change" so no more wonder.
Umm... this might be a stupid question but, I can't use this code to retrieve data from one plugin., like this one: http://api.bukget.org/api2/bukkit/plugin/aboot
@FoxyShadoww your new url returns a completely different json string. You should use a different class (or dynamic) to deserialize this response.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.