Click here to Skip to main content
14,236,234 members

jParser PCL .NET Json Parser

Rate this:
5.00 (3 votes)
Please Sign up or sign in to vote.
5.00 (3 votes)
15 Feb 2016CPOL
Fast n Easy Way to parse Json data

Introduction

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. For more information, visit: www.json.org.

Why JSON?

  • Easy to read
  • Small size compared to XML
  • Most sites API use Json to get and post data

Background

I wrote this library to do the following:

  • Newtonsoft.Json Style to get Values, like data["some_str"][ANY_INT]
  • Small Files Size ~11kb
  • Support Datatype of Json: String, Int, Double, Bool, Null, Array, Object
  • Support for loop
  • Small code and easy to understand
  • Portable Class Library. PCL
  • Use Only Loop and If statement to parse data so that it is easy in porting to another language
  • Support all types of Json even Json Array which in Newtonsoft we must parse using JArrays but in this array, it's parsing all types in one method

Using the Code

Let's say we have this simple Json data:

{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

To parse this text:

var result = jParser.parser.Parse(JSON_DATA_STRING);

To get value from result, let's say we want to get:"CreateNewDoc()" value

string data = result["menu"]["popup"]["menuitem"][0]["onclick"];

To get type of a specific object, let's say we want to get Typeof value Of menuitem

Type tp = result["menu"]["popup"]["menuitem"].GetType();

tp will be List<object>

To loop over items in result:

foreach(var itm in result)
{
    // some code here
}

Comparison

History

  • Version 1.0.2.0 -- Rewritten from scratch, parsing using loop, add support for IEnumerable, fix bugs
  • Version 0.1.2.0 -- First release, parsing using recursion

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ahmed Amer Jaf
Software Developer
Iraq Iraq
Hello, I'm Ahmed From Iraq.
I'm Developer Since When i Was 16 Years Old.
And I'm Studying Computer Science In Al-Mansour University College.
I Programming In C# And Visual Basic.NET
I Have A Basic Knowledge In C++.

Comments and Discussions

 
GeneralMy Vote of 5 Pin
aarif moh shaikh15-Feb-16 17:26
professionalaarif moh shaikh15-Feb-16 17:26 
GeneralRe: My Vote of 5 Pin
Ahmed Amer Jaf18-Feb-16 2:39
memberAhmed Amer Jaf18-Feb-16 2:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Article
Posted 15 Feb 2016

Stats

9.6K views
10 downloads
1 bookmarked