开发者

How can I parse a JavaScript object with jQuery

开发者 https://www.devze.com 2022-12-21 18:53 出处:网络
What\'s the foreach equivalent in jQuery (开发者_StackOverflow社区JavaScript) to parse through a JSON object and return key/value pairs?What do you mean by \"JSON object\"? JSON is a text format for s

What's the foreach equivalent in jQuery (开发者_StackOverflow社区JavaScript) to parse through a JSON object and return key/value pairs?


What do you mean by "JSON object"? JSON is a text format for serialising objects.

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:

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. 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:

var obj = eval(jsonString);
0

精彩评论

暂无评论...
验证码 换一张
取 消