Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / WPF

jParser PCL .NET Json Parser

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
15 Feb 2016CPOL1 min read 18K   1   3
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:

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

To parse this text:

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

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

JavaScript
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

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

tp will be List<object>

To loop over items in result:

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

Comparison

Image 1 Image 2

Image 3 Image 4

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)


Written By
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

 
Questionwhere IS the code? Pin
eblaschka17-Apr-21 16:58
eblaschka17-Apr-21 16:58 
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
Ahmed Amer Jaf18-Feb-16 2:39 
Thank you Smile | :)

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.