JSON parse dan stringify, menggunakan browser Native JSON dan JavaScript framework


Cara untuk mengubah String menjadi JSON object (deserialize) dan mengubah JSON object menjadi String (serialize) menggunakan browser native JSON support dan JavaScript Library.

Daftar browser yang sudah mensupport native JSON:

  • Mozilla Firefox 3.5+
  • Internet Explorer 8+
  • Opera 10.5+
  • Webkit browser (Google Chrome, Apple Safari)

MooTools

http://mootools.net/docs/core/Utilities/JSON

var jsonString = '{"blog":"tscf.wordpress.com"}';

var obj = JSON.decode(jsonString);
alert(obj.blog);

var str = JSON.encode(obj);
alert(str);

live at jsFiddle

jQuery

http://api.jquery.com/jQuery.parseJSON/

var jsonString = '{"blog":"tscf.wordpress.com"}';
var obj = jQuery.parseJSON(jsonString);
alert(obj.blog);

live at jsFiddle

Saat ini jQuery tidak memiliki function untuk mengkonversi JSON object menjadi string, pilihan yang ada adalah menggunakan browser native JSON (yang pasti lebih cepat ketika proses eksekusi) atau menggunakan json2.js jika tetap ingin mensupport browser yang belum memiliki native JSON support.

Prototype

http://api.prototypejs.org/language/String/prototype/evalJSON/
http://api.prototypejs.org/language/Object/toJSON/

var jsonString = '{"blog":"tscf.wordpress.com"}';

var obj = jsonString.evalJSON();
alert(obj.blog);

var str = Object.toJSON(obj);
alert(str);

live at jsFiddle

YUI 3

http://developer.yahoo.com/yui/3/json/

YUI().use('json', function (Y) {
    var jsonString = '{"blog":"tscf.wordpress.com"}';

    var obj = Y.JSON.parse(jsonString);
    alert(obj.blog);

    var str = Y.JSON.stringify(obj);
    alert(str);
});

live at jsFiddle

Glow

http://www.bbc.co.uk/glow/docs/1.7/api/glow.data.shtml

var jsonString = '{"blog":"tscf.wordpress.com"}';

var obj = glow.data.decodeJson(jsonString);
alert(obj.blog);

var str = glow.data.encodeJson(obj);
alert(str);

live at jsFiddle

No library – pure javascript

https://developer.mozilla.org/En/Using_native_JSON
http://msdn.microsoft.com/en-us/library/cc836466%28VS.85%29.aspx
http://msdn.microsoft.com/en-us/library/cc836459%28VS.85%29.aspx

var jsonString = '{"blog":"tscf.wordpress.com"}';

var obj = JSON.parse(jsonString);
alert(obj.blog);

var str = JSON.stringify(obj);
alert(str);

live at jsFiddle

dojo toolkit

http://dojotoolkit.org/reference-guide/dojo/toJson.html
http://dojotoolkit.org/reference-guide/dojo/fromJson.html

var jsonString = '{"blog":"tscf.wordpress.com"}';

var obj = dojo.fromJson(jsonString);
alert(obj.blog);

var str = dojo.toJson(obj);
alert(str);

live at jsFiddle

ExtJS

http://dev.sencha.com/deploy/ext-4.0.0/docs/api/Ext.JSON.html

var jsonString = '{"blog":"tscf.wordpress.com"}';

var obj = Ext.decode(jsonString);
alert(obj.blog);

var str = Ext.encode(obj);
alert(str);

live at jsFiddle

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: