function formatJson(json, options) {
var reg = null,
formatted = '',
pad = 0,
PADDING = ' '; // one can also use '\t' or a different number of spaces
// optional settings
options = options || {};
// remove newline where '{' or '[' follows ':'
options.newlineAfterColonIfBeforeBraceOrBracket = (options.newlineAfterColonIfBeforeBraceOrBracket === true) ? true : false;
// use a space after a colon
options.spaceAfterColon = (options.spaceAfterColon === false) ? false : true;
// begin formatting...
// make sure we start with the JSON as a string
if (typeof json !== 'string') {
json = JSON.stringify(json);
}
// parse and stringify in order to remove extra whitespace
json = JSON.parse(json);
json = JSON.stringify(json);
// add newline before and after curly braces
reg = /([\{\}])/g;
json = json.replace(reg, '\r\n$1\r\n');
// add newline before and after square brackets
reg = /([\[\]])/g;
json = json.replace(reg, '\r\n$1\r\n');
// add newline after comma
reg = /(\,)/g;
json = json.replace(reg, '$1\r\n');
// remove multiple newlines
reg = /(\r\n\r\n)/g;
json = json.replace(reg, '\r\n');
// remove newlines before commas
reg = /\r\n\,/g;
json = json.replace(reg, ',');
// optional formatting...
if (!options.newlineAfterColonIfBeforeBraceOrBracket) {
reg = /\:\r\n\{/g;
json = json.replace(reg, ':{');
reg = /\:\r\n\[/g;
json = json.replace(reg, ':[');
}
if (options.spaceAfterColon) {
reg = /\:/g;
json = json.replace(reg, ': ');
}
$.each(json.split('\r\n'), function(index, node) {
var i = 0,
indent = 0,
padding = '';
if (node.match(/\{$/) || node.match(/\[$/)) {
indent = 1;
} else if (node.match(/\}/) || node.match(/\]/)) {
if (pad !== 0) {
pad -= 1;
}
} else {
indent = 0;
}
for (i = 0; i < pad; i++) {
padding += PADDING;
}
formatted += padding + node + '\r\n';
pad += indent;
});
return formatted;
}
json 字符串格式化
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門符喝,熙熙樓的掌柜王于貴愁眉苦臉地迎上來闪彼,“玉大人,你說我怎么就攤上這事协饲∥吠螅” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵茉稠,是天一觀的道長描馅。 經(jīng)常有香客問我,道長而线,這世上最難降的妖魔是什么铭污? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮膀篮,結(jié)果婚禮上嘹狞,老公的妹妹穿的比我還像新娘。我一直安慰自己誓竿,他們只是感情好磅网,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著筷屡,像睡著了一般涧偷。 火紅的嫁衣襯著肌膚如雪簸喂。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼竿奏,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了腥放?” 一聲冷哼從身側(cè)響起泛啸,我...
- 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎秃症,沒想到半個(gè)月后候址,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡种柑,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年岗仑,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片聚请。...
- 正文 年R本政府宣布煤傍,位于F島的核電站盖文,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏蚯姆。R本人自食惡果不足惜五续,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望龄恋。 院中可真熱鬧疙驾,春花似錦、人聲如沸篙挽。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽铣卡。三九已至链韭,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間煮落,已是汗流浹背敞峭。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長得像轿衔,于是被迫代替她去往敵國和親沉迹。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- JsonStrToDic.h JsonStrToDic.m
- /*! * @brief 把格式化的JSON格式的字符串轉(zhuǎn)換成字典 * @param jsonString JSO...
- - (NSDictionary *)dictionaryWithJsonString:(NSString*)jso...
- /*! @brief 把格式化的JSON格式的字符串轉(zhuǎn)換成字典 @param jsonString JSON格式的...