版权声明:本文为博主原创文章,未经博主允许不得转载。
[html] view plain copy
- var str = {"code": "","svcname": "","version": "","component": "","category": "","requestMsg": [{"fieldName": "userName","type": "simple","required": "true"},{"fieldName": "age","type": "simple","required": "true"}]}
一、校验字符串是否是JSON格式:
[html] view plain copy
- function isJsonFormat(str) {
- try {
- $.parseJSON(str);
- } catch (e) {
- return false;
- }
- return true;
- }
二、将json格式的字符串格式化输出,先将json字符串转为对象,然后将此对象以json格式化输出:
[html] view plain copy
- JSON.stringify(JSON.parse(json), null, "\t")
[html] view plain copy
- JSON.stringify(JSON.parse(json), null, 4)
PS:\t:代表缩进一个tab;4:代表缩进4个空格
格式化后的输出:
[html] view plain copy
- {
- "code": "",
- "svcname": "",
- "version": "",
- "component": "",
- "category": "",
- "requestMsg": [
- {
- "fieldName": "userName",
- "type": "simple",
- "required": "true"
- },
- {
- "fieldName": "age",
- "type": "simple",
- "required": "true"
- }
- ]
- }
推荐一个好的JSON在线解析工具:http://www.json.cn/
时间: 2024-12-14 18:45:25