Posts Tagged ‘JSON’

JSON to C# object to JSON

Posted: September 8, 2011 in C#, Javascript, JQuery, JSON
Tags: , , ,

public class SearchFilter
public string SearchCol { get; set; }
public string SearchOp { get; set; }
public string SearchText { get; set; }
public int PageNo { get; set; }
using System;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
public class JSONHelper
public static T Deserialise<T>(string json)
T obj = Activator.CreateInstance<T>();
using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
obj = (T)serializer.ReadObject(ms);
return obj;

public static string Serialize<T>(T obj)
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
using (MemoryStream ms = new MemoryStream())
serializer.WriteObject(ms, obj);
return Encoding.Default.GetString(ms.ToArray());
Usage –

SearchFilter lSearchFilter = JSONHelper.Deserialise<SearchFilter>(“{‘SearchCol’:’Col1′,’SearchOp’:’contains’,’SearchText’:’hi’}”);

function ToJSONKeyValueFormat(pKey, pValue, pIsString) {
return ((pIsString)? (“\”” + pKey + “\”:\”” + pValue + “\””) : (“\”” + pKey + “\”:” + pValue));
function ToJSONFormat() {
var lJSONString = “”;
for (var i = 0; i < arguments.length; i++) {
lJSONString = ((i === 0)? “{” + arguments[i] : lJSONString = lJSONString + “,” + arguments[i]);
return ((lJSONString !== “”) ? lJSONString + “}” : “”);


ToJSONFormat(ToJSONKeyValueFormat(“Key1”, “Val1”, true), ToJSONKeyValueFormat(“Key2”, 2, false))



Use JavaScriptSerializer to convert JSON object to C# object and backwards. It is very useful to build AJAX based web pages for thinner network traffic and for better client side html computation …

using System.Web.Script.Serialization;

public class Employee
 public string Name { get; set; }
        public string Age { get; set; }
        public string ID { get; set; }

Employee oEmployee1 = new Employee { Name = “name1”, ID = “1”, Age = “5” };
Employee oEmployee2 = new Employee { Name = “name2”, ID = “2”, Age = “7” };
Employee oEmployee3 = new Employee { Name = “name3”, ID = “3”, Age = “8” };
List<Employee> oList = new List<Employee>() { oEmployee1, oEmployee2, oEmployee3 };

JavaScriptSerializer oSerializer = new JavaScriptSerializer();
string sJSON = oSerializer.Serialize(oList);
List<Employee> oNewList = (List<Employee>)oSerializer.Deserialize(sJSON, typeof(List<Employee>));
Response.Write(oNewList.Count + “”);

string sJSON2 = oSerializer.Serialize(oEmployee1);
Employee emp = (Employee)oSerializer.Deserialize(sJSON2, typeof(Employee));
Response.Write(emp.Name + “”);

JSON Validator – Utility

Posted: March 29, 2011 in JSON

Following link helps you to validate your JSON string  and there are many other sites which does this.

JSONView extension/addon helps developers to parse and view JSON document directly on Chrom/Firefox browsers …

Firefox Addon – (Firebug Addon also has a tab for seeing JSON response)

Chrome Extension –

It seems IE does not have such Addon but there is work around explained @



Posted: June 17, 2010 in JSON

JSON (an acronym for JavaScript Object Notation) is a lightweight text-based open standard designed for human-readable data interchange. It is derived from the JavaScript programming language for representing simple data structures and associative arrays, called objects.

The JSON format is often used for serializing and transmitting structured data over a network connection. It is primarily used to transmit data between a server and web application, serving as an alternative to XML.

Note: var mydata = eval(‘(‘+myJSONText+’)’); will work. But eval has security issues so use JSON.parse which will throw an exception if the text contains anything dangerous.

JSONP or “JSON with padding” is a complement to the base JSON data format, a usage pattern that allows a page to request and more meaningfully use JSON from a server other than the primary server. JSONP is an alternative to a more recent method called Cross-Origin Resource Sharing.

Under the same origin policy, a web page served from cannot normally connect to or communicate with a server other than An exception is HTML <script> tags. Taking advantage of the open policy for <script> tags, some pages use them to retrieve JSON from other origins

To see how that works, let’s consider a URL that, when requested, returns a JSON statement. In other words, a browser requesting the URL would receive something like:

 {“Name”: “Cheeso”, “Rank”: 7}
The Basic Idea: Retrieving JSON via Script Tags
It’s possible to specify any URL, including a URL that returns JSON, as the src attribute for a <script> tag.

Specifying a URL that returns plain JSON as the src-attribute for a script tag, would embed a data statement into a browser page. It’s just data, and when evaluated within the browser’s javascript execution context, it has no externally detectable effect.

One way to make that script have an effect is to use it as the argument to a function. invoke( {“Name”: “Cheeso”, “Rank”: 7}) actually does something, if invoke() is a function in Javascript.

And that is how JSONP works. With JSONP, the browser provides a JavaScript “prefix” to the server in the src URL for the script tag; by convention, the browser provides the prefix as a named query string argument in its request to the server, e.g.,

 <script type=”text/javascript”

The server then wraps its JSON response with this prefix, or “padding”, before sending it to the browser. When the browser receives the wrapped response from the server it is now a script, rather than simply a data declaration. In this example, what is received is

   parseResponse({“Name”: “Cheeso”, “Rank”: 7})

…which can cause a change of state within the browser’s execution context, because it invokes a method.

Read more from here