PHP程序员站--PHP编程开发平台
 当前位置:主页 >> 网页制作 >> Javascript >> 

jQuery JSON插件json_encode and json_decode

jQuery JSON插件json_encode and json_decode

来源:PHP程序员站  作者:PHP程序员站  发布时间:2011-06-19
一个jquery的json插件,可以实现json的编码和解码 This plugin makes it simple to convert to and from JSON: var thing = {plugin: 'jquery-json', version: 1.3}; var encoded = $.toJSON(thing); // '{plugin: jquery-json, version: 1.3}' var name = $.evalJSON(e

一个jquery的json插件,可以实现json的编码和解码

This plugin makes it simple to convert to and from JSON:
var thing = {plugin: 'jquery-json', version: 1.3};
var encoded = $.toJSON(thing); // '{“plugin”: “jquery-json”, “version”: 1.3}'
var name = $.evalJSON(encoded).plugin; // “jquery-json”
var version = $.evalJSON(encoded).version; // 1.3


This plugin exposes five new functions onto the $, or jQuery object:
* toJSON: Serializes a javascript object, number, string, or arry into JSON.
* compactJSON: Serializes as toJSON, but takes out some spaces, making the result that much shorter.
* evalJSON: Converts from JSON to Javascript, quickly, and is trivial.
* secureEvalJSON: Converts from JSON to Javascript, but does so while checking to see if the source is actually JSON, and not with other Javascript statements thrown in.
* quoteString: Places quotes around a string, and inteligently escapes any quote, backslash, or control characters.

ajax例子:
$(document).ready(function(){
var data = new Object();
data.hello = “Hello”;
data.world = 'World';
data.worked = ” it worked “;
data.somebool = true;
data.array = new Array(“he\”ll\”o”, '”World”');
var dataString = $.toJSON(data);
$.post('phpfile.php', {data: dataString}, function(res){


var obj = $.evalJSON(res);
if(obj.somebool === true)
$(“#result”).html(obj.hello + ' ' + obj.array[1] + obj.worked + “. Message from PHP: “+obj.php_message);
});
});
http://code.google.com/p/jquery-json/


延伸阅读:
什么是JSON
JSON 入门指南
实例详解PHP serialize与JSON解析
PHP中JSON的应用
PHP中JSON技巧讲解
PHP json_encode函数进行中文转换
JSON Jquery Codeigniter 使用详解
在PHP使用json_encode
jquery JSON的解析方式
jQuery新版本加载json注意事项
jQuery插件—把xml转化为json插件

最新文章
推荐阅读
月点击排行榜
PHP程序员站 Copyright © 2007-2010,PHPERZ.COM All Rights Reserved 粤ICP备07503606号