Custom Search

News World

Jun 1, 2009

File Extension JSON better then the other script source code that use for big data size

JSON Abbreviation from JavaScript Object Notation. File Extension JSON is interchange data format as basically notation in JavaScript. In a few narration said that File Extension JSON better then XML as interchange data format.

Is that true? I tray to make a simple test make sure that issues in ASP.Net Web Service.

How to Run JSON files?

With Windows, Mac OS and Linux Operating System

  • Run with a JSON parser

How to Edit JSON files script?

  • Edit with a text editor (Use the editor file that related in Operating System. )


I’ve use two parameter those are stream transmitted and response time.

I use the Employee object with EmpId data, Name, Sex and Title the first. This object will serialize to JSON and XML as WebService Value return.

namespace JSONSample
{
public class Employee
{
public string EmpId { get; set; }
public string Name { get; set; }
public string Title { get; set; }
public char Sex { get; set; }
}
}

And then I make the web service with two method that get Employee in format JSON and XML.

sing System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.Script.Services;

namespace JSONSample
{
///


/// Summary description for WebService1
///

[WebService(Namespace = "">http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[ScriptService]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public Employee GetEmployee(string EmpId)
{
var emp = new Employee();
emp.EmpId = EmpId;
emp.Name = "Ahmad Masykur";
emp.Sex = 'M';
emp.Title = "Analyst, Application Architecture";
return emp;
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Employee GetEmployeeJSON(string EmpId)
{
var emp = new Employee();
emp.EmpId = EmpId;
emp.Name = "Ahmad Masykur";
emp.Sex = 'M';
emp.Title = "Analyst, Application Architecture";
return emp;
}
}
}

And then, create JavaScript code to execute both of method in WebService that created before.

var displayElement1;
var displayElement2;

// Initializes global variables and session state.
function pageLoad()
{
displayElement1 = $get("ResultId1");
displayElement2 = $get("ResultId2");
}
function getEmployee() {
JSONSample.WebService1.GetEmployee("894683", OnSucceeded, OnFailed);
JSONSample.WebService1.GetEmployeeJSON("894683", OnSucceeded, OnFailed);
}

// Callback function invoked on successful
// completion of the page method.
function OnSucceeded(result, userContext, methodName)
{
if (methodName=="GetEmployee") { // xml
displayElement1.innerHTML = result.documentElement.selectSingleNode('//Name').text;
}
else { // json
displayElement2.innerHTML = result.Name;
}
}

// Callback function invoked on failure
// of the page method.
function OnFailed(error, userContext, methodName)
{
if(error !== null)
{
displayElement.innerHTML = "An error occurred: " +
error.get_message();
}
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

And then ASP.NET AJAX that will registray JavaScript and WebService then WebService can execute with JavaScript by ASP.NET AJAX Proxy Service.




No comments:

IT Conversations

Moneycontrol Latest News

Latest new pages on Computer Hope

Latest from Infoworld

Door Lock

Door Lock Import Top Door Lock from China Contact Quality Manufacturers Now