【发布时间】:2021-08-30 21:55:25
【问题描述】:
我的 JSON 是这样存储在 MySQL 中的......
{'profile':'sweet', 'count':38},{'profile':'bitter', 'count':31},{'profile':'green', 'count':22}
当它作为 JSON 从 Express 返回时,它看起来像这样......
[{"JSON":"{'profile':'sweet', 'count':38},{'profile':'bitter', 'count':31},{'profile':'green', 'count':22}"}]
根据 JSONLint.com,这是有效的 JSON
让我感到震惊的是在 HTML javascript 中对其进行迭代...
我在 Javascript 中有这个 ....
fProfiles_JSON = JSON.parse(xhr.responseText);
console.log('Type of '+ typeof fProfiles_JSON); //yields "object"
console.log('My object', fProfiles_JSON);
console.log('LENGTH ', fProfiles_JSON.length); // Yields "1"
我知道我必须以某种方式迭代此对象类型以获取“配置文件”和“计数”值,但老实说,我不确定如何因为长度值为“1”。我知道这可能很简单,我只是没有看到它。有人能指出我正确的方向吗?
【问题讨论】:
-
这不是有效的 JSON。如果你有多个对象,它们需要放在一个数组中。
-
如果您使用 console.log 记录 fProfiles_JSON 会是什么样子?
-
它是有效的 JSON,因为它被打包成一个字符串......但显然是一个字符串。
-
你能告诉我们
xhr- 它返回了什么? -
和
xhr.responseText?那到底是什么?
标签: javascript json express