var oJSON = [
{businessNum:2,merchantBusinessId:456},
{businessNum:1,merchantBusinessId:154},
{businessNum:3,merchantBusinessId:336},
{businessNum:5,merchantBusinessId:108}
];
制一个函数 fn(json); 出来如一下这种形式(字符串):
"[{\"amount\":\"1\",\"merbusinessId\":\"92\"},{\"amount\":\"1\",\"merbusinessId\":\"94\"},{\"amount\":\"1\",\"merbusinessId\":\"107\"}]"
11 回答

慕森卡
TA贡献1806条经验 获得超8个赞
看你是不是在做题,或者题目有要求。没有要求就是json。stringdify完事。
如果是要考你实现json字符串的序列化。那就麻烦一点。
类似伪代码:
fn(json){
var result='';
if array.isarray(json) result='[]';
else
result='{}'
for (var item in json){
if(json[item] is array or object)
result="${item}":${fn(json[item])}
}
else
result="${item}":${json[item] is bool?true or false; is string "json[item]"; is numer?json[item]}
}

Smart猫小萌
TA贡献1911条经验 获得超7个赞
function transJson(json){
let res=JSON.stringify(json).replace(/"/g,"\"");
return res;
}
传入的json数字能先自行转为字符串不

临摹微笑
TA贡献1982条经验 获得超2个赞
fn(oJSON){
let newJson = []
for(let [index, json] of oJSON.entries()){
for(let key in json){
if(newJson[index] == undefined){
newJson[index] = {}
}
if(key == 'businessNum' || key == 'merchantBusinessId'){
newJson[index][key] = json[key] + ''
}
}
}
newJson = JSON.stringify(newJson)
newJson = newJson.replace(/"/g, '\\"')
return `"${newJson}"`
};
添加回答
举报
0/150
提交
取消