So, I have a json file and I want to get the names of all the fields in the file using javascript. Is there a better way other than using eval and then using reflection on the resulting object? Or does that make no sense? Thanks.
所以,我有一个json文件,我想用javascript获取文件中所有字段的名称。除了使用eval然后在结果对象上使用反射之外,还有更好的方法吗?或者这没有意义?谢谢。
5 个解决方案
#1
You should do what some frameworks use, like:
你应该做一些框架使用的东西,比如:
http://www.prototypejs.org/api/string/evalJSON
It checks if the JSON is sanitized. But other than that its just using old plain eval() at the end.
它检查JSON是否已清理。但除此之外,它只是在最后使用旧的普通eval()。
#2
Unless you want to parse the text somehow I would suggest the easiest way being to eval it and the loop though it to get all the field names.
除非你想以某种方式解析文本,否则我会建议最简单的方法来评估它,并通过循环来获取所有字段名称。
#3
Note, there is a proposal for a JSON Schema, defined in JSON. They even have a validator up on Google code, so you might find use of this even now.
注意,有一个JSON模式的提议,在JSON中定义。他们甚至在Google代码上都有验证器,所以即使是现在你也可以使用它。
#4
Well, it would get tricky, because of nesting and such. It depends on your structure. And javascript's "reflection" doesn't have any performance hit, so feel free to walk through the object's members. Or you could roll your own parser based on the JSON specs.
好吧,它会变得棘手,因为嵌套等等。这取决于你的结构。并且javascript的“反射”没有任何性能损失,因此请随意浏览对象的成员。或者你可以根据JSON规范推出自己的解析器。
#5
A JSON file doesn't really need to comply with a consistent schema. There can be ragged rows; elements can vary in sequence across rows; any row can have any elements (or none). All in hierarchical-node fashion. So coming up with what you want by ad hoc inspection (reflection) could be pretty dicey.
JSON文件实际上不需要遵循一致的架构。可能有破烂的行;元素可以按行顺序变化;任何行都可以有任何元素(或没有)。全部采用分层节点方式。所以通过临时检查(反思)提出你想要的东西可能会非常冒险。
Not to say that any parties involved in using a JSON format couldn't put more structural requrements in place, but then you'd have the schema to start with.
并不是说参与使用JSON格式的任何一方都不能提出更多的结构要求,但是你必须先从架构开始。
#1
You should do what some frameworks use, like:
你应该做一些框架使用的东西,比如:
http://www.prototypejs.org/api/string/evalJSON
It checks if the JSON is sanitized. But other than that its just using old plain eval() at the end.
它检查JSON是否已清理。但除此之外,它只是在最后使用旧的普通eval()。
#2
Unless you want to parse the text somehow I would suggest the easiest way being to eval it and the loop though it to get all the field names.
除非你想以某种方式解析文本,否则我会建议最简单的方法来评估它,并通过循环来获取所有字段名称。
#3
Note, there is a proposal for a JSON Schema, defined in JSON. They even have a validator up on Google code, so you might find use of this even now.
注意,有一个JSON模式的提议,在JSON中定义。他们甚至在Google代码上都有验证器,所以即使是现在你也可以使用它。
#4
Well, it would get tricky, because of nesting and such. It depends on your structure. And javascript's "reflection" doesn't have any performance hit, so feel free to walk through the object's members. Or you could roll your own parser based on the JSON specs.
好吧,它会变得棘手,因为嵌套等等。这取决于你的结构。并且javascript的“反射”没有任何性能损失,因此请随意浏览对象的成员。或者你可以根据JSON规范推出自己的解析器。
#5
A JSON file doesn't really need to comply with a consistent schema. There can be ragged rows; elements can vary in sequence across rows; any row can have any elements (or none). All in hierarchical-node fashion. So coming up with what you want by ad hoc inspection (reflection) could be pretty dicey.
JSON文件实际上不需要遵循一致的架构。可能有破烂的行;元素可以按行顺序变化;任何行都可以有任何元素(或没有)。全部采用分层节点方式。所以通过临时检查(反思)提出你想要的东西可能会非常冒险。
Not to say that any parties involved in using a JSON format couldn't put more structural requrements in place, but then you'd have the schema to start with.
并不是说参与使用JSON格式的任何一方都不能提出更多的结构要求,但是你必须先从架构开始。