JSON - Serialized Language and Cross-Domain Scripting

#### Installation


This is nice stand-alone library for .NET environment


  1. Right click on project
  2. Add Reference
  3. Browse Newtonsoft.Json.dll


JSON library is ready as extension of apache.

  1. Download php_json.dll

http://www.aurore.net/projects/php-json/ 2. Copy php_json.dll in ext directory 3. Modify php.ini (Add extension=php_json.dll) 4. Restart Apache

#### Description

What is JSON? JSON is just serialization language. That means you convert your class or array in PHP, Java, .NET, etc… into JavaScript, so that you can pass the data around between different platform. Since the data object is already in JavaScript, it makes AJAX guys happy. Somebody claims JSON as “Light-weight version of XML”. Because you do not parse it.

As for cross-site scripting, it’s nothing to do with JSON. But I explore the advantages of the combination.

The first example demonstrates JSON object’s serialization/deserialization in PHP language. I just encode array into JSON, and the other section decode from JSON. As you see, you can put these parts in remote server. This client can be another plat form.

The second example is ASP.NET example is code to demonstrate how to achive cross-domain data transfer. Basic model is like this

  1. ASP.NET spit out JavaScript library which returns a JSON object. The JSON object is just serialized string array object.
  2. In html page, javascript include the aspx page as library, and call the function to obtain JSON object. The object is just array of string in javascript.

#### Service / Client Test in PHP

echo json_encode($arr);
echo '<hr />';

#### Building JSON Service - JSONTest.cs File

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Newtonsoft.Json;

public partial class JSONTest : System.Web.UI.Page {
public string mOutput;
protected void Page_Load(object sender, EventArgs e) {
string[] arr = new string[] { "apple","banana","orange","strawberry" };
mOutput = JavaScriptConvert.SerializeObject(arr);


#### Building JSON Service - JSONTest.aspx Page

This is assumed to be used as .js library. getObj return parsed JSON object.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="JSONTest.aspx.cs" Inherits="JSONTest" %>
// You do not need to wrap in JS function if you use in another language like PHP, Python, etc...
function getObj() {
return eval('(<%=mOutput %>)');
//return eval('(["apple","banana","orange","strawberry"])');

#### Consuming JSON in JavaScript

<script type="text/javascript" src="http://%28aspx%20page%20location%29/JSONTest.aspx"></script>
<script language="javascript" type="text/javascript">  
 var obj = getObj();  
 document.write(obj[0] + "<br />");  
 document.write(obj[1] + "<br />");  
 document.write(obj[2] + "<br />");  
 document.write(obj[3] + "<br />");  

By: gavi on: