请注意,这用于lodash
检测数组和对象,这是另一种将“叶子”对象保持在一行上的方法:
_.jsonPretty = function(obj, indent) {
if(!indent) indent = 2;
return JSON.stringify(obj, function(k,v) {
//Check if this is a leaf-object with no child Arrays or Objects:
for(var p in v) {
if(_.isArray(v[p]) || _.isObject(v[p])) {
return v;
}
}
return JSON.stringify(v);
//Cleanup the escaped strings mess the above generated:
}, indent).replace(/\\/g, '')
.replace(/\"\[/g, '[')
.replace(/\]\"/g,']')
.replace(/\"\{/g, '{')
.replace(/\}\"/g,'}');
};
就像这样使用它:
_.jsonPretty(yourObjectToStringify);
这是之前的一个例子......
{
"type": "light-item",
"name": "Waiting",
"ringSeqLooping": true,
"ringSeqHoldLast": false,
"ringSteps": [
{
"type": "light-step",
"time": 1,
"audioClipName": "Off",
"audioVolume": 1,
"lights": [
{
"state": "FadeOn",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
}
]
},
{
"type": "light-step",
"time": "0.5",
"audioClipName": "Off",
"audioVolume": 1,
"lights": [
{
"state": "FadeOff",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
}
]
}
],
"stripSeqLooping": true,
"stripSeqHoldLast": false,
"stripSteps": [
{
"type": "light-step",
"time": "2",
"audioClipName": "Off",
"audioVolume": 1,
"lights": [
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "FadeOn",
"color": "#fff"
},
{
"state": "FadeOn",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
}
]
},
{
"type": "light-step",
"time": "2",
"audioClipName": "Off",
"audioVolume": 1,
"lights": [
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "FadeOff",
"color": "#fff"
},
{
"state": "FadeOff",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
}
]
}
]
}
...以及之后:
{
"type": "light-item",
"name": "Waiting",
"ringSeqLooping": "true",
"ringSeqHoldLast": "false",
"ringSteps": [
{
"type": "light-step",
"time": "1",
"audioClipName": "Off",
"audioVolume": "1",
"lights": [
{"state":"FadeOn","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"}
]
},
{
"type": "light-step",
"time": "0.5",
"audioClipName": "Off",
"audioVolume": "1",
"lights": [
{"state":"FadeOff","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"}
]
}
],
"stripSeqLooping": "true",
"stripSeqHoldLast": "false",
"stripSteps": [
{
"type": "light-step",
"time": "2",
"audioClipName": "Off",
"audioVolume": "1",
"lights": [
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"FadeOn","color":"#fff"},
{"state":"FadeOn","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"}
]
},
{
"type": "light-step",
"time": "2",
"audioClipName": "Off",
"audioVolume": "1",
"lights": [
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"FadeOff","color":"#fff"},
{"state":"FadeOff","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"}
]
}
]
}