JS ES6拼接字符串

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\"}]"
湖上湖
浏览 1213回答 11
11回答

慕森卡

看你是不是在做题,或者题目有要求。没有要求就是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]} }

梵蒂冈之花

感谢回复, 可能需要fn(json);  出来形式和题目一模一样,只是数字变了..

慕哥9229398

@dudu: 这个方法好,学习了

一只萌萌小番薯

@dudu: 这方法NB,可能是我题目描述不准确,现在补了.   感谢

Smart猫小萌

function transJson(json){ let res=JSON.stringify(json).replace(/"/g,"\""); return res; } 传入的json数字能先自行转为字符串不

繁星点点滴滴

感谢回复。 不是要都答案。

临摹微笑

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}"` };

隔江千里

点错了,这个函数可以满足要求!!~
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript