var str = {"code": "","svcname": "","version": "","component": "","category": "","requestMsg": [{"fieldName": "userName","type": "simple","required": "true"},{"fieldName": "age","type": "simple","required": "true"}]}
一、校验字符串是否是JSON格式:
function isJsonFormat(str) {
try {
$.parseJSON(str);
} catch (e) {
return false;
}
return true;
}
二、将json格式的字符串格式化输出,先将json字符串转为对象,然后将此对象以json格式化输出:
JSON.stringify(JSON.parse(json), null, "\t")
JSON.stringify(JSON.parse(json), null, 4)
PS:\t:代表缩进一个tab;4:代表缩进4个空格
格式化后的输出:
{
"code": "",
"svcname": "",
"version": "",
"component": "",
"category": "",
"requestMsg": [
{
"fieldName": "userName",
"type": "simple",
"required": "true"
},
{
"fieldName": "age",
"type": "simple",
"required": "true"
}
]
}
|