错误 JSON stringify 格式
Error JSON stringify format
var email_recept=document.getElementsByClassName("email");
var counting_email_recept=email_recept.length;
var array_mail=[];
var myJSON=[];
for(var i=0;i<counting_email_recept;i++)
{
array_mail[i]=email_recept[i].value;
myJSON[i]=JSON.stringify({ email: array_mail[i] });
}
console.log(myJSON);
result :
["{"email":""}", "{"email":""}"]
i want to make like this : [{"email":""}, {"email":""}]
你的问题有点模棱两可,但我想你可能想要...
var myJSON=[];
for(var i=0;i<counting_email_recept;i++) {
array_mail[i]=email_recept[i].value;
myJSON[i]={ email: array_mail[i] };
}
myJSON = JSON.stringify(myJSON);
// '[{"email":"a@a.a"},{"email":"b@b.b"}]'
尝试使用此代码:
var email_recept=document.getElementsByClassName("email");
var counting_email_recept=email_recept.length;
var array_mail=[];
var myJSON=new array();
for(var i=0;i<counting_email_recept;i++)
{
array_mail[i]=email_recept[i].value;
myjson[i] = { email: array_mail[i] };
}
myJSON = JSON.stringify(myJSON);
console.log(myJSON);
你会得到想要的输出!
var email_recept=document.getElementsByClassName("email");
var counting_email_recept=email_recept.length;
var array_mail=[];
var myJSON=[];
for(var i=0;i<counting_email_recept;i++)
{
array_mail[i]=email_recept[i].value;
myJSON[i]=JSON.stringify({ email: array_mail[i] });
}
console.log(myJSON);
result :
["{"email":""}", "{"email":""}"]
i want to make like this : [{"email":""}, {"email":""}]
你的问题有点模棱两可,但我想你可能想要...
var myJSON=[];
for(var i=0;i<counting_email_recept;i++) {
array_mail[i]=email_recept[i].value;
myJSON[i]={ email: array_mail[i] };
}
myJSON = JSON.stringify(myJSON);
// '[{"email":"a@a.a"},{"email":"b@b.b"}]'
尝试使用此代码:
var email_recept=document.getElementsByClassName("email");
var counting_email_recept=email_recept.length;
var array_mail=[];
var myJSON=new array();
for(var i=0;i<counting_email_recept;i++)
{
array_mail[i]=email_recept[i].value;
myjson[i] = { email: array_mail[i] };
}
myJSON = JSON.stringify(myJSON);
console.log(myJSON);
你会得到想要的输出!