方法一:
//数据格式是这样的:
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);