来源: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
- "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
- ["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
- {"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
- {"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
- 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
- Hello World
- 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
- Hello World
- My hovercraft is full of eels.
- 3.147
本文介绍了JSON数据格式及其在JavaScript中的应用,重点讲解了如何使用JSON.stringify()将JavaScript对象转化为JSON字符串,以及如何通过JSON.parse()将JSON字符串还原为JavaScript对象。示例包括简单的字符串、数组及复杂对象的转换。
1万+

被折叠的 条评论
为什么被折叠?



