The Simplest way to Parse JSON Data in C# using JSON.Net

SimpleJSONNetExample

using System;
using Newtonsoft.Json.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = "{\"employees\": [{ \"firstName\":\"John\" , \"lastName\":\"Doe\" },{ \"firstName\":\"Anna\" , \"lastName\":\"Smith\" }]}";

            JObject o = JObject.Parse(json);
            Console.WriteLine(o["employees"][0]);
            Console.WriteLine(o["employees"][1]["firstName"]);
            Console.WriteLine(o["employees"][1]["lastName"]);
            Console.ReadLine();
        }
    }
}

I was looking for a simple way to parse JSON data using C#, and was led to this example from MSDN: http://msdn.microsoft.com/en-us/library/bb412179(v=vs.110).aspx on how to Serialize and Deserialize JSON Data.  DataContracts, DataMembers, on and on, seemed a bit much for just trying to extract some data from a string.

JSON.net is a library from James Newton-King.  By far the easiest .NET JSON Parser available and according to James, it’s also the fastest.

Resources:
JSON.NET on CodePlex
http://json.codeplex.com/

An Introduction to JavaScript Object Notation (JSON) in JavaScript and .NET
http://msdn.microsoft.com/en-us/library/bb299886.aspx

 

 

WebMethods For Use With Visual Studio and AJAX

The webmethod tag is an attribute added to a Public methods to indicate the method should be exposed as part of an XML Web service.  Below is an example of applying the WebMethod attribute.


<WebMethod()> _
Public Shared Function FunctionName(ByVal Parameter As String) As ObjectName
Dim ObjectName As Object = Object
Return ObjectName
End Function

The Web method can now be called via javascript on the client. Below is an example of calling the webmethod from the client.

PageMethods.FunctionName(Parameter1, onComplete, OnTimeOut, onError);

Here is a great link with additional information for web methods:

http://www.novologies.com/post/2009/05/06/Cleaner-Faster-Ajax-in-ASPNET.aspx

JQuery Show Hide Div Example Tutorial

<script type=”text/javascript” language=”javascript”>
<!–

$(document).ready(function(){

$(‘#btnAddNew’).click(function(){
var isVisible = $(‘#divNew’).is(‘:visible’);
if (isVisible == false){
$(“#divNew”).animate({ height: ‘show’, opacity: ‘show’ }, ‘fast’);
} else{
$(“#divNew”).animate({ height: ‘hide’, opacity: ‘hide’ }, ‘fast’);
}
return false;
});
});

–>
</script>