我在迭代一个json对象时遇到了一些困难。有一些嵌套的字段,这让我感到困惑......
这是我的JSON数据
{
"_embedded": {
"request_fields": [{
"reference": null,
"name": "Qual será o serviço?",
"label": "Qual será o serviço?",
"placeholder": "Qual será o serviço?",
"values": {
"Coloração": "Coloração",
"Corte": "Corte",
"Escova ": "Escova ",
"Escova progressiva/definitiva": "Escova progressiva/definitiva",
"Luzes": "Luzes"
}
}, {
"reference": null,
"name": "Para quem será o serviço?",
"label": "Para quem será o serviço?",
"placeholder": "Para quem será o serviço?",
"values": {
"Criança": "Criança",
"Homem": "Homem",
"Mulher": "Mulher"
}
}]
}
}
这是我到目前为止所尝试的 - 更新
for (key in response) {
for (i in response[key].request_fields) {
// console.log(response[key].request_fields[i].values);
document.getElementById("form").innerHTML += "<div class='section'>" +
"<label>" + response[key].request_fields[i].label + "</label>" +
"<label>" for (e in response[key].request_fields[i].values) { response[key].request_fields[i].values[e] } "</label>" +
"<label>" + response[key].request_fields[i].values{0} + "</label>" +
"</div>" ;
}
}
现在我要
了未捕获的SyntaxError:
的意外标记
在我的第三个&#34;为&#34;打印&#34;值&#34;
答案 0 :(得分:0)
这是因为索引i
不在值中,您需要另一个迭代器或特定值,或者只需要json它,或者将它连接到字符串。
for (key in response) {
for (i in response[key].request_fields) {
console.log(response[key].request_fields[i].values);
var values=(()=>{
var out="";
for(var ii in response[key].request_fields[i].values){
out+=ii+": "+response[key].request_fields[i].values[ii]+"\n";
}
return out;
})();
document.getElementById("form").innerHTML += "<div class='section'>" + "<label>" + response[key].request_fields[i].label + "</label>" + "<label>" +
values + "</label>" + "</div>";
}
}