简体   繁体   English

如何使用jQuery解析JavaScript对象

[英]How can I parse a JavaScript object with jQuery

什么是jQuery(JavaScript)中的foreach等效解析JSON对象并返回键/值对?

What do you mean by "JSON object"? 你是什​​么意思“JSON对象”? JSON is a text format for serialising objects. JSON是用于序列化对象的文本格式。

If you want to loop through the properties in an object that you got from deserialising a JSON string, you just loop through the property names: 如果要遍历通过反序列化JSON字符串获得的对象中的属性,只需遍历属性名称:

for (key in object) {
  alert('key='+key+', value='+object[key]);
}

If you want to get the data from a JSON string, the easiest way is to parse it into an object. 如果要从JSON字符串中获取数据,最简单的方法是将其解析为对象。 You can do that using an existing library, for example jQuery, or if you trust the contents of the string completely you can simply evaluate it: 您可以使用现有库(例如jQuery)执行此操作,或者如果您完全信任该字符串的内容,则可以简单地对其进行评估:

var obj = eval(jsonString);

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

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