数据解析的方法

解析数据的模块body-parser (Node.js的身体解析中间件)

installation

$ npm install body-parser

API

var bodyParser = require('body-parser');

JSON.parse() json格式的字符串 -----解析------JSON对象

var t = '{"a": "hello"}' ;
JSON.parse(t) ; // {a: "hello"}

更多实例:
JSON.parse('{}'); // {}
JSON.parse('true'); // true
JSON.parse('"foo"'); // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null'); // null

JSON.stringify() json格式的对象----解析-----JSON字符串

var t = {"a": "hello"} ;
JSON.stringify(t) ; // ' {"a": "hello"} '

更多实例:
var str = {"name":"菜鸟教程", "site":"http://www.runoob.com"}

  • 只有一个参数情况:
    str_pretty1 = JSON.stringify(str);
    document.write("

    " + str_pretty1 + "
    " );
    {"name":"菜鸟教程", "site":"http://www.runoob.com"}

  • 使用参数情况:
    str_pretty2 = JSON.stringify(str, null, 4); //使用四个空格缩进
    document.write("

    " + str_pretty2 + "
    " ); // pre 用于格式化输出
    { "name": "菜鸟教程", "site": "http://www.runoob.com" }

你可能感兴趣的:(数据解析的方法)