查看一个简单的jQuery的例子来遍历一个JavaScript数组对象。

[js] view plaincopy

 
  1. var json = [
  2. {"id":"1","tagName":"apple"},
  3. {"id":"2","tagName":"orange"},
  4. {"id":"3","tagName":"banana"},
  5. {"id":"4","tagName":"watermelon"},
  6. {"id":"5","tagName":"pineapple"}
  7. ];
  8. $.each(json, function(idx, obj) {
  9. alert(obj.tagName);
  10. });

上面的代码片断工作正常,提示 “apple”, “orange” … 等,如预期一样。
问题: JSON 字符串

下面的例子中,声明了一个JSON字符串(随附单或双引号)直接地。

[js] view
plain
copy

 
  1. var json = '[{"id":"1","tagName":"apple"},{"id":"2","tagName":"orange"},
  2. {"id":"3","tagName":"banana"},{"id":"4","tagName":"watermelon"},
  3. {"id":"5","tagName":"pineapple"}]';
  4. $.each(json, function(idx, obj) {
  5. alert(obj.tagName);
  6. });

在Chrome中,它显示在控制台下面的错误:

Uncaught TypeError: Cannot use 'in' operator to search for '156' 
in [{"id":"1","tagName":"apple"}...

解决方案:JSON字符串转换为JavaScript对象。
要修复它,通过标准JSON.parse()或jQuery 的 $.parseJSON 将其转换为JavaScript对象。

[js] view
plain
copy

 
  1. var json = '[{"id":"1","tagName":"apple"},{"id":"2","tagName":"orange"},
  2. {"id":"3","tagName":"banana"},{"id":"4","tagName":"watermelon"},
  3. {"id":"5","tagName":"pineapple"}]';
  4. $.each(JSON.parse(json), function(idx, obj) {
  5. alert(obj.tagName);
  6. });
  7. //or
  8. $.each($.parseJSON(json), function(idx, obj) {
  9. alert(obj.tagName);
  10. });

JQuery $.each遍历JSON字符串报Uncaught TypeError:Cannot use 'in' operator to search for的更多相关文章

  1. $.each遍历JSON字符串和 Uncaught TypeError: Cannot use 'in' operator to search for '156错误

    现在页面和后端交互都流行使用json了  自己记录一下解析字符串的方法,要不老是忘记!!!! success: function (data) { //data是后台传过来的字符串 $.each(JS ...

  2. jquery each报 Uncaught TypeError: Cannot use 'in' operator to search for错误

    用$.each()来遍历后台传过来的json数据.直接遍历传过来的数据时就发生 Uncaught TypeError: Cannot use 'in' operator to search for 这 ...

  3. iOS解析JSON字符串报错Error Domain=NSCocoaErrorDomain Code=3840 "Invalid escape sequence around character 586."

    将服务器返回的JSON string转化成字典时报错: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid escape sequence ...

  4. TypeError: Cannot use 'in' operator to search for 'length' in....

    前台页面读取商品属性是字符串形式,数据库中存储商品属性是集合形式,前台数据存入数据库中数据格式会自动转,后台数据回显到前台数据格式需要手动转换,否则会报异常 错误信息提示:

  5. $.each遍历json对象

    查看一个简单的jQuery的例子来遍历一个JavaScript数组对象. var json = [ {"id":"1","tagName": ...

  6. $each 遍历json字符串

    $.each遍历json对象   查看一个简单的jQuery的例子来遍历一个JavaScript数组对象. var json = [ {"id":"1",&qu ...

  7. $.each遍历json对象(java将对象转化为json格式以及将json解析为普通对象)

    查看一个简单的jQuery的例子来遍历一个JavaScript数组对象. var json = [ {"id":"1","tagName": ...

  8. ESL python调用C模块时传递unicode字符串报错问题解决

    在是用freeswitch时利用ESL的python调用时传递字符串报错 TypeError: in method 'ESLconnection_api', argument 2 of type 'c ...

  9. 项目中遇到Uncaught TypeError: Converting circular structure to JSON报错问题

    最近公司项目中出现一个报错Uncaught TypeError: Converting circular structure to JSON,,根据上述报错可以知道代码是运行到JSON.stringi ...

随机推荐

  1. LeetCode: Palindrome Partitioning II 解题报告

    Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...

  2. 使用 libvirt创建和管理KVM虚拟机

      1. libvirt介绍    Libvirt是一个软件集合,便于使用者管理虚拟机和其他虚拟化功能,比如存储和网络接口管理等等.Libvirt概括起来包括一个API库.一个 daemon(libv ...

  3. Node.js学习笔记(1)--一个最简单的服务器请求

    说明(2017-5-2 10:27:03): 1. 需要安装node,http://nodejs.cn/download/ 2. 安装完后,在cmd里输入node -v可以查看版本. 3. 代码foo ...

  4. AlphaGo论文的译文,用深度神经网络和树搜索征服围棋:Mastering the game of Go with deep neural networks and tree search

    转载请声明 http://blog.csdn.net/u013390476/article/details/50925347 前言: 围棋的英文是 the game of Go,标题翻译为:<用 ...

  5. Tomcat配置JMX远程监控(Windown7 Linxu)

    一:Window7下配置方式. 1.配置catalina.bat 在第一行加入下面配置 注意下面这些配置要在一行,注意包含空格. set JAVA_OPTS=-Dcom.sun.management. ...

  6. PHP——大话PHP设计模式——PSR-0规范

  7. 唯一id算法

    https://blog.csdn.net/guodongcc322/article/details/55211273 https://blog.csdn.net/weixin_36751895/ar ...

  8. Microsoft Azure

    Service Bus - Event Hub - Event Hubs Programming Guide - Service Bus Event Hubs Getting Started (Sam ...

  9. 在R中运行Shell命令脚本(Call shell commands from R)

    aaa.R Args <- commandArgs()cat("Args[1]=",Args[1],"\n")cat("Args[2]=&quo ...

  10. How to install SharePoint 2013 on Windows Server 2012 R2

    [Update 26.02.2014] Many thanks to everybody commented on this post. As Falk already mentioned in th ...