方法一:
//數據格式是這樣的:
var data = '[{"id":30348079,"name":"表1","score":"1","pid":"0"},{"id":30348079,"name":"表1","score":"1","pid":"0"}]';
console.log(
"---"
+jsonData);
// 下邊操作
// :替換:"
// ,替換",
// }]替換"}]
// }",{替換 "},{
jsonData = jsonData.replace(/\:/g,"\:\
""
).replace(/\,/g,
"\"\,"
).replace(/\}\]/g,
"\"\}\]"
).replace(/\}\
"\,\{/g,"
\
"\}\,\{"
);
console.log(
"++++"
+jsonData);
//最后轉回json對象
data = eval(
'('
+ jsonData +
')'
);
console.log(data);
方法二:
// 待處理的json對象
var
jsonObj = {
"title"
:
"\u660c\u5409"
,
"content"
:
""
,
"point"
:
"87.997349|43.732302"
,
"isOpen"
:
"0"
,
"icon"
:
"w:21,h:21,l:0,t:0,x:6,lb:5"
};
// json的值將被臨時儲存在這個變量中
var
keyValue =
""
;
// 處理好的json字符串
var
jsonStr =
""
;
for
(
var
key
in
jsonObj) {
keyValue += key +
':"'
+ jsonObj[key] +
'",'
;
}
// 去除最后一個逗號
keyValue = keyValue.substring(0,keyValue.length - 1);
jsonStr =
"{"
+ keyValue +
"}"
;
console.log(jsonStr);