[英]JSON array of strings foreach
Example JSON:示例 JSON:
{
"name": "Size",
"options": [
"S",
"M",
"L"
]
}
I do not understand why options
is parsed as a string like SML
and foreach does not work我不明白为什么
options
被解析为像SML
这样的字符串,而 foreach 不起作用
SOLVED:解决了:
Instead of doing a foreach I used a for我没有使用 foreach,而是使用了 for
const jsonData = '{"name": "Size","options": ["S","M","L"]}'
const data = JSON.parse(jsonData)
// Foreach not be used in array of strings, this was the error
//data.options.forEach((option) => {
//
//})
// For works fine
for (const i = 0; i < data.options.length; i++) {
const option = data.options[i]
}
It seems to work fine:它似乎工作正常:
var json = `{
"name": "Size",
"options": [
"S",
"M",
"L"
]
}`;
var obj = JSON.parse(json);
console.log(obj.options);
You don't show how you parse it, how you access the obj.options
array, what you get and what you expect.你没有展示你如何解析它,你如何访问
obj.options
数组,你得到了什么以及你期望什么。 It's impossible to tell you what's wrong with your code that you don't show.不可能告诉您您没有显示的代码有什么问题。 The JSON is fine and I showed you the correct way to parse it that works as expected.
JSON 很好,我向您展示了解析它的正确方法,它按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.