Parsing JSON API in C#

c#, json, json.net, parsing

Solution

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

Problem

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?

Original source