Ibraheem Osama Mohamed (ابراهيم اسامه محمد)

Welcome to my blog :)

C# and JSON

with 11 comments

JSON is syntax for storing and exchanging text information. Much like XML. JSON refers to  JavaScript Object Notation.
JSON is smaller than XML, and faster and easier to parse.
Send a message from server to client JSON now is the better way. let’s make a little comparison between JSON and XML

This is a comparison I Copied it from W3schools.com

Much Like XML                                                        Much Unlike XML

  • JSON is plain text                                                                                                      No end tag
  • JSON is “self-describing” (human readable)                                                   Shorter
  • JSON is hierarchical (values within values)                                                   Quicker to read and write
  • JSON can be parsed by JavaScript                                                                     Can be parsed using built-in JavaScript eval()
  • JSON data can be transported using AJAX                                                     Uses arrays and No reserved word

This Post will help you read a Json file in C#.but before we start you must know about JSON syntax Let’s get started 🙂

  1.  Open the note pad or any documents application.
  2. Write the JSON array you want to read in the text file.(I wrote [{“First Name”:”Ibraheem”},{“Last Name”:”Osama”}] )
  3. Close the file and put it in the application directory in the bin-> debug.
  4. Open Visual Studio, Choose new WPF Application.
  5. In the UI drag a Button and a TextBox.
  6. Add a Click event for the button
  7. Before entering in the C# Code you must add a reference to System.Json so we can begin writing our Code 🙂
  8. This is the Code to write in the Button Click Event :
    private void button1_Click(object sender, RoutedEventArgs e) 
    //Open the file              
    var stream = File.OpenText("json file.txt"); 
    //Read the file              
    string st = stream.ReadToEnd();                           
    var jsonArray = JsonArray.Parse(st);              
    foreach (var item in jsonArray)              
    JsonObject ob = new JsonObject(item);                   
    foreach (var t in ob.Values)                  
    JsonObject oo = new JsonObject(t);                       
    foreach (var x in oo)                      
    textBox1.AppendText(x.Key + " : " + x.Value + "\n");                      

    Out put of the first method

    Let’s illustrate the Code First open the file then read the file contents.Now we parse the file string into a J-SON array.
    Now we have the array we must extract from it the items. So we Iterate on the arrays to extract the items and We converted the items into JSON object after that We iterated on the JSON object to extract another object then we get the values and keys of the last object

    Now we are going to make our JSON Object and JSON Array inside the program

    private void button2_Click(object sender, RoutedEventArgs e)         
    KeyValuePair<stringJsonValue> pair = new KeyValuePair<stringJsonValue>("FName","Ibraheem");             
    KeyValuePair<stringJsonValue> pair2 = new KeyValuePair<stringJsonValue>("LName""Osama");              
    List<KeyValuePair<stringJsonValue>> list = new List<KeyValuePair<stringJsonValue>>();             
    JsonObject jObject = new JsonObject(list);             
    var stream = new StreamWriter("json out file.txt");             
    foreach (var x in jObject)             
    //  textBox1.AppendText(x.Key + " : " + x.Value + "\n");                 
    textBox1.AppendText(x.ToString() + "\n");                 
    stream.WriteLine(x.ToString() + "\n");             
    JsonArray jarray = new JsonArray("item1","item2","Item3");             
    foreach (var x in jarray)             

    out out of the second method

    Now you’ll be able to parse a JSON file just understand the JSON arrays and the JSON object
    You can also write the JSON format in files like the second method 🙂 


    Ibraheem Osama Mohamed
    Twitter : @IbraheemOsamaMo 

Written by Ibraheem Osama Mohamed

April 30, 2012 at 8:16 pm

Posted in C#, Json

Tagged with ,

11 Responses

Subscribe to comments with RSS.

  1. I have witnessed that intelligent real estate agents everywhere you go are starting to warm up to FSBO Marketing and advertising. They are recognizing that it’s more than simply placing a sign in the front area. It’s really regarding building interactions with these dealers who at some time will become buyers. So, while you give your time and efforts to encouraging these suppliers go it alone – the “Law regarding Reciprocity” kicks in. Interesting blog post.

    Dakota Giagni

    July 15, 2012 at 9:21 am

  2. Thanks for your article on the travel industry. We would also like contribute that if you are a senior taking into consideration traveling, its absolutely imperative that you buy travel insurance for senior citizens. When traveling, elderly people are at biggest risk being in need of a healthcare emergency. Obtaining the right insurance policies package on your age group can protect your health and give you peace of mind.

    Shelby Bast

    July 17, 2012 at 9:09 am

  3. Hey there! This post couldn’t be written any better! Reading this post reminds me of my previous room mate! He always kept talking about this. I will forward this page to him. Fairly certain he will have a good read. Many thanks for sharing!

  4. A further issue is that video games are typically serious naturally with the principal focus on understanding rather than enjoyment. Although, it comes with an entertainment facet to keep your kids engaged, each game is normally designed to work on a specific experience or programs, such as math concepts or science. Thanks for your publication.

    Wendie Striegel

    July 21, 2012 at 12:30 am

  5. Hey this is kind of of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding skills so I wanted to get guidance from someone with experience. Any help would be greatly appreciated!

    home business page

    September 21, 2012 at 3:42 pm

  6. Not what I was hunting for but superb anyway! Well done!

    coconut oil

    September 28, 2012 at 7:00 pm

  7. So cool…

    The information mentioned in the article are some of the best available…


    October 3, 2012 at 6:56 pm

  8. I like the valuable info you supply for your articles. I will bookmark your weblog and test again here frequently. I’m somewhat sure I’ll be told many new stuff right right here! Best of luck for the following!

    Donn Orleans

    October 4, 2012 at 9:57 pm

  9. Pretty nice post. I just stumbled upon your weblog and
    wanted to say that I’ve really loved browsing your weblog posts. In any case I will be subscribing for your feed and I’m hoping
    you write again very soon!


    January 19, 2013 at 4:30 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: