美文网首页
数据解析的方法

数据解析的方法

作者: 吧啦啦小汤圆 | 来源:发表于2016-10-31 22:38 被阅读8次

解析数据的模块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("<pre>" + str_pretty1 + "</pre>" );
    {"name":"菜鸟教程", "site":"http://www.runoob.com"}

  • 使用参数情况:
    str_pretty2 = JSON.stringify(str, null, 4); //使用四个空格缩进
    document.write("<pre>" + str_pretty2 + "</pre>" ); // pre 用于格式化输出
    { "name": "菜鸟教程", "site": "http://www.runoob.com" }

相关文章

网友评论

      本文标题:数据解析的方法

      本文链接:https://www.haomeiwen.com/subject/qrnputtx.html