【问题标题】:How do I parse json array using .each with no outer name?如何使用没有外部名称的 .each 解析 json 数组?
【发布时间】:2011-12-18 05:16:08
【问题描述】:

我想使用 jquery 解析这个 json,每个都有一个循环。我只见过 json 示例,其中它包含在带有诸如结果之类的标题的数组中。如何解析 json 如下...?

$.each(json.results, function() { console.log(this['title']); }); [{"title":"关于我们","url_title":"about_us","entry_id":"155","channel_id":"2","author_id":"1","status":"open" ,"entry_date":"1287508538","edit_date":"20101209122240","expiration_date":"0","about_body":"我们是最伟大的。","about_image":"","about_staff_title":"乔","about_extended":"更多关于我们的信息。","color":"d12626"},{"title":"联系我们","url_title":"contact_us","entry_id":"223","channel_id ":"2","author_id":"1","status":"open","entry_date":"1291918929","edit_date":"20101209122310","expiration_date":"0","about_body": "给我们发电子邮件。","about_image":"","about_staff_title":"Bob","about_extended":"","color":"080480"}]

【问题讨论】:

  • 您的预期结果是什么?
  • 喜欢这个? jsfiddle.net/TuhHt
  • 感谢 Jared 完美!

标签: jquery arrays json parsing


【解决方案1】:
$.each(json, function() { 控制台.log(this.title); });

【讨论】:

  • @digitalbart - 这与我的评论基本相同。
  • @JaredFarrish 你想发表答案吗,我会接受的
  • @digitalbart - 不,没关系。我们同时发布的。
【解决方案2】:

不应该只是$.each(json, function() ... ) 吗?您的 JSON 是一个数组,没有包装器,因此只需将其直接传递给 $.each()

【讨论】:

  • 我不确定你将它直接传递给 $.each() 是什么?你能详细说明一下吗
  • @Sudhir 和 @Jared 显示的内容 - 如果您的 JSON 是一个数组,并且您将其加载到名为 json 的变量中,只需将 json 作为第一个参数传递给 $.each,而不是通过json.results
  • 谢谢,我想多了。
猜你喜欢
  • 2020-08-09
  • 2020-01-08
  • 2017-05-10
  • 2020-03-28
  • 2017-06-10
  • 2010-12-03
  • 2019-10-26
  • 2021-11-24
  • 1970-01-01
相关资源
最近更新 更多