简体   繁体   English

使用console.log(object)并遍历对象键时,JSON键有所不同

[英]JSON keys different when using console.log(object) and iterating over the object keys

I am having some trouble with a JSON object I receive from my server. 我从服务器收到的JSON对象遇到问题。 Using console.log on the JSON object correctly shows the JSON object I sent from the server, but when I try to iterate over the keys it starts acting strange. 在JSON对象上使用console.log可以正确显示我从服务器发送的JSON对象,但是当我尝试遍历键时,它开始表现得很奇怪。 See below. 见下文。

> console.log(promotions);
{"promotion1":{"color":"white","backgroundColor":"red","text":"from promotion server"},"promotion2":{"color":"purple","backgroundColor":"yellow","text":"from promotion server2"},"promotion3":{"color":"green","backgroundColor":"black","text":"from promotion server3"}}

> for (p in promotions) { console.log(p); }
0
1
2
3
4
... (continues)
264
265
266
bold
strip
stripColors
trap
zalgo
zebra
rainbow
random
america
reset
dim
italic
underline
inverse
hidden
strikethrough
black
... (continues)
magentaBG
cyanBG
whiteBG

A few more details: 更多细节:

I am sending a request from a ExpressJS server using http.request to another ExpressJS server, which then returns the "promotion" JSON object. 我正在使用http.request从ExpressJS服务器向另一个ExpressJS服务器发送请求,然后该服务器返回“促销” JSON对象。 I have not been able to tell if it somehow gets corrupted underway (I might have set the encoding wrong?). 我还无法判断它是否以某种方式被破坏了(我可能设置了错误的编码?)。

Testing using the same JSON object but hardcoded instead of sending it from the other server, gives the correct response. 使用相同的JSON对象但经过硬编码而不是从其他服务器发送来进行测试,可以给出正确的响应。

Thanks! 谢谢!

Your promotions value is the string representation of the JSON. 您的promotions价值是JSON的字符串表示形式。 So when you do your for loop, it iterates through it as a string. 因此,当您执行for循环时,它将作为字符串进行遍历。

 var promotions = '{"promotion1":{"color":"white","backgroundColor":"red","text":"from promotion server"},"promotion2":{"color":"purple","backgroundColor":"yellow","text":"from promotion server2"},"promotion3":{"color":"green","backgroundColor":"black","text":"from promotion server3"}}'; for (var p in promotions) { console.log(p); } 

For why it does this, see this related question (although the truth is its just a weird consequence of the fact that strings are arrays behind the scenes in which case you should look at this answer ) 对于为什么这样做,请参阅此相关问题 (尽管事实是,字符串是幕后数组,这是一个奇怪的结果,在这种情况下,您应该查看此答案

If you do 如果你这样做

 var promotions = '{"promotion1":{"color":"white","backgroundColor":"red","text":"from promotion server"},"promotion2":{"color":"purple","backgroundColor":"yellow","text":"from promotion server2"},"promotion3":{"color":"green","backgroundColor":"black","text":"from promotion server3"}}'; // convert string representation of JSON into actual object var promotionsObject = JSON.parse(promotions); for (var p in promotionsObject) { console.log(p); } 

It should work as expected. 它应该按预期工作。 Just keep in mind if you want to go through each object, and print the properties of those, you'll need to do extra work. 请记住,如果要遍历每个对象并打印这些对象的属性,则需要做额外的工作。 See Printing nested JSON without using variable names 请参阅不使用变量名打印嵌套的JSON

Try filtering out the properties that belongs to prototype 尝试过滤掉属于原型的属性

 for (var p in promotions) { if (promotions.hasOwnProperty(p)) { console.log(p); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM