Node(23) JSON

来源:互联网 发布:淘宝开网店的必备条件 编辑:程序博客网 时间:2024/06/07 10:23

JSON example  profiles.js

module.exports = {Peter : {name: 'Peter Parker',age: '18',gender: 'M',grade: 'B'},Alex : {name: 'Alex Giggersn',age: '23',gender: 'F',grade: 'B+'}}

JSON functions

JSON.parse(text [, reviver])

JSON.stringify(JSONvalue [, replacer] [, space])

var profiles = require( './profiles');//convert json to string, replace 'name' with 'fullname'profiles = JSON.stringify(profiles).replace( /name/g, 'fullname' );//parse string back to jsonprofiles = JSON.parse( profiles );//fix Alex's fullnameprofiles.Alex.fullname = "Alex Giggerson";//log Alex infoconsole.log( profiles.Alex );


Posting Json Object

Like other types, when you want to send object by POST method, you have to specify the header's mime type

xmlhttp.setRequestHeader("Content-Type","application/json");
xmlhttp.setRequestHeader("Content-Length",jsonString.length); 


原创粉丝点击