Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing JSON API in C#

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?

like image 522
Cistoran Avatar asked May 08 '12 22:05

Cistoran


People also ask

What is JSON object in C?

JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc. JSON stands for JavaScript Object Notation. The format was specified by Douglas Crockford.

Can I use JSON in C?

Parsing JSON in C using microjson Developed originally for server-browser communication, the use of JSON has since expanded into a universal data interchange format. This tutorial will provide a simple introduction to parsing JSON strings in the C programming language using the microjson library.

What is JSON parsing example?

JSON (JavaScript Object Notation) is a straightforward data exchange format to interchange the server's data, and it is a better alternative for XML. This is because JSON is a lightweight and structured language.

What is JSMN?

jsmn (pronounced like 'jasmine') is a minimalistic JSON parser in C. It can be easily integrated into resource-limited or embedded projects. You can find more information about JSON format at json.org. Library sources are available at https://github.com/zserge/jsmn.


1 Answers

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);
like image 198
sjokko Avatar answered Oct 22 '22 04:10

sjokko