Parsing JSON API in C#

26,622

Solution 1

Did you try to JArray instead? Depending on what kind of object you are trying to return

WebClient client = new WebClient();
var data = client.DownloadString("");
var jArray = JArray.Parse(data);

Solution 2

JSON requires brackets for arrays and commas between multiple objects.

This is per the JSON standard. I also recommend using JSON.net via NuGet instead of the native JSON parser unless it is overkill and you cannot have the extra bloat.

For example, your parsing a file with two seperate JSON objects - the following does not work per the JSON standard (lacks a comma between the 2 objects and the two objects are not encapsulated by brackets):

{"status":1,"error":null}
{"status":2,"error":null}

The following 3 JSON objects parsed from a file does work (has brackets for multiple objects and commas between objects):

[{"glossary": {"title": "fun glossary","SeeAlso": ["GML", "XML"]},
{"glossary": {"title": "grey glossary","SeeAlso": ["GML", "XML"]},
{"glossary": {"title": "blue glossary","SeeAlso": ["GML", "XML"]}]
Share:
26,622
Cistoran
Author by

Cistoran

I know nothing :D

Updated on May 21, 2020

Comments

  • Cistoran
    Cistoran almost 4 years

    so I'm fairly new to programming but am looking to go much deeper with it. I recently started to get involved in a project to create a WinForm program for a website that uses an API system in JSON.

    I've never used an API before so I'm not quite sure how it works but after looking at it for a few minutes I seem to have the gist of it. What I don't get is how exactly parsing JSON in C# works.

    I found this link after a little google searching. And I got it working (somewhat) with this code.

    static void Main(string[] args)
    {
            WebClient c = new WebClient();
            var vLogin = c.DownloadString("https://www.openraid.us/index.php/api/login/username/password");
            //Returns string 
            //{"status":1,"error":null,"token":"250-1336515541-c48d354d96e06d488d1a2530071ef07c9532da26"} 
            //Token = random, no decisive length*/
            JObject o = JObject.Parse(vLogin);
            Console.WriteLine("Login Status: " + o["status"]);
            String sToken = "" + o["token"];
            Console.WriteLine(sToken);
            Console.WriteLine("");
            //Breaks after this
            var myRaids = c.DownloadString("https://www.openraid.us/index.php/api/myraids/"+sToken);
            JObject r = JObject.Parse(myRaids); //error occurs here
            String sEventId = "" + r["event_id"];
            Console.WriteLine("Event ID: " + sEventId);
            Console.ReadLine();
    }
    

    So to me it looks like I have parsing 1 page done and handled, but when I move onto the second I get this error.

    Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.

    So I guess my question is, how do I parse more than 1 page or call of JSON and what would be the easiest way to break up each section of the JSON object (Such as status, error, and token, into C# strings?

  • Jonno
    Jonno over 11 years
    +1 it's probably in the doco somewhere but one would expect the JSON.Net API is a bit more intuitive... thanks for this answer :-)