JSON Parse and Stringify

来源:http://rjb.soc.port.ac.uk/talks/js/json/

This is an illustration of the parse and stringify methods included in ECMA-262 edition 5 - older browsers do not include this capability and require (small) additional libraries to deliver these capabilities securely.

JSON
JavaScript Object Notation was "discovered" by Douglas Crockford and is a key capability that has helped increase the profile as a useful language.
JSON.stringify
The stringify method of the JSON object transforms JavaScript Objects into
JSON.parse
The parse method transforms a JSON object literal string into a JavaScript variable.

Stringify

Stringify Hello World

Here, we show how to use JSON object encoding on string "Hello World" using the stringify method.

input

var message = "Hello World";
var stringified = JSON.stringify(message);
document.write( stringified );
	

output

  1. "Hello World"

Stringify An Array

Here, we show the JSON object encoding an array using the stringify method.

input

var messages = new Array(2);
messages[0] = "Hello World";
messages[1] = "My hovercraft is full of eels.";
var stringified = JSON.stringify(messages);
document.write( stringified );
	

output

  1. ["Hello World","My hovercraft is full of eels."]

Stringify An Object

Here, we show the JSON object encoding an object using the stringify method.

input

var messages = {
	greeting: "Hello World",
	password: "My hovercraft is full of eels.",
	pi: 3.147
}
messages.badger="mushroom";

var stringified = JSON.stringify(messages);
document.write( stringified );
	

output

  1. {"greeting":"Hello World","password":"My hovercraft is full of eels.","pi":3.147,"badger":"mushroom"}

Stringify More Complex Objects

Here, we show something more complex.

input

var m = {}
m.from="rich.boakes@port.ac.uk";
m.to="bryan.carpenter@port.ac.uk";
m.subject="An example";
m.transmission =[];
m.transmission[0] = {};
m.transmission[0].srchost="rjb.dsg.port.ac.uk";
m.transmission[0].desthost="smtp.port.ac.uk";
m.transmission[0].time="17/2/2010 11:00";
m.transmission[1] = {
	srchost: "smtp.port.ac.uk",
	desthost: "mailstore.port.ac.uk",
	time: "17/2/2010 11:01"
};

var stringified = JSON.stringify(m);
document.write( stringified );
	

output

  1. {"from":"rich.boakes@port.ac.uk","to":"bryan.carpenter@port.ac.uk","subject":"An example","transmission":[{"srchost":"rjb.dsg.port.ac.uk","desthost":"smtp.port.ac.uk","time":"17/2/2010 11:00"},{"srchost":"smtp.port.ac.uk","desthost":"mailstore.port.ac.uk","time":"17/2/2010 11:01"}]}

Parse

Parse Hello World

Here, the string generated by the Hello World method is parsed back into JavaScript.

input

var json = '"Hello World"';  // note the extra quotes!
var parsed = JSON.parse(json);
document.write( parsed );
	

output

  1. Hello World

Parse An Array

Here, we show the JSON object reconstituting array using the parse method.

input

var json = '["Hello World","My hovercraft is full of eels."]';
var parsed = JSON.parse(json);
document.write( parsed[0] );
document.write( parsed[1] );
	

output

  1. Hello World
  2. My hovercraft is full of eels.

Parse An Object

Here, we show the JSON object unpacking an object using the parse method.

input

var json = 
	'{'+
	'"greeting":"Hello World",'+
	'"password":"My hovercraft is full of eels.",'+
	'"pi":3.147'+
	'}';

var parsed = JSON.parse(json);
document.write( parsed.greeting );
document.write( parsed.password );
document.write( parsed.pi );
	

output

  1. Hello World
  2. My hovercraft is full of eels.
  3. 3.147
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值