资讯专栏INFORMATION COLUMN

solve this Javascript error when converting JSON t

alphahans / 277人阅读

摘要:这个问题已经提问者已经解决,虽然解决的办法不尽人意,但是他采纳了公众赞同的答案。解决转换时指针循环问题这段代码大体背景就是从中获取到的结果其实是一个对象数组中嵌套对象数组。用上面最佳答案就是以上

How do I solve this Javascript error when converting JSON to string?

This q come from stackoverflow see:
http://stackoverflow.com/questions/4954242/how-do-i-solve-this-javascript-error-when-converting-json-to-string

Uncaught TypeError: Converting circular structure to JSON
The object I"m trying to stringify is this (I logged it in javascript console):

Object
GsearchResultClass: "GlocalSearch"
accuracy: "8"
addressLines: Array[2]
city: "Cupertino"
content: ""
country: "United States"
ddUrl: "http://www.google.com/maps?source=uds&daddr=10825+North+Wolfe+Road,+Cupertino,+CA+(Southland+Flavor+…"
ddUrlFromHere: "http://www.google.com/maps?source=uds&saddr=10825+North+Wolfe+Road,+Cupertino,+CA+(Southland+Flavor+…"
ddUrlToHere: "http://www.google.com/maps?source=uds&daddr=10825+North+Wolfe+Road,+Cupertino,+CA+(Southland+Flavor+…"
html: HTMLDivElement
lat: "37.335405"
listingType: "local"
lng: "-122.015386"
maxAge: 604800
phoneNumbers: Array[1]
region: "CA"
staticMapUrl: "http://maps.google.com/maps/api/staticmap?maptype=roadmap&format=gif&sensor=false&size=150x100&zoom=…"
streetAddress: "10825 North Wolfe Road"
title: "Southland Flavor Cafe"
titleNoFormatting: "Southland Flavor Cafe"
url: "http://www.google.com/maps/place?source=uds&q=stinky&cid=9384294304761453216"
viewportmode: "computed"
__proto__: Object
And I"m doing it like this:

JSON.stringify(theobject);
 这个问题已经提问者已经解决,虽然解决的办法不尽人意,但是他采纳了公众赞同的答案。
 
 作者也在这个问题上花了一部分时间,下面阐述一下我的解决方法。
 
  var v =$("#tagse").select2("data");

        //解决转换时指针循环问题
        var arr1 = [];
        for(var i=0;i

这段代码大体背景就是从select2中获取到的结果其实是一个对象数组中嵌套对象数组。
但是通过JSON.stringify来讲对象转换成json有的时候回提示错误:Uncaught TypeError: Converting circular structure to JSON,没错,这个错误就是指针循环了导致的。

因为从select2中或得的对象里面包含了大量select2的api信息,有的信息存在指针循环的现象,可以通过网上一些人的办法通过替换这样问题的对象,也可以采用我的办法,就是将自己需要的信息提取出来,一目了然,不会再发生前面提到的错误。

用stackoverflow上面最佳答案就是:

An object is referencing itself somewhere; hence the message "circular structure." I suspect it might be in the HTMLDivElement. Are you using this only for debugging purposes or do you actually want to do something meaningful with this JSON? If you"re just using it for debugging, most modern JavaScript debuggers will let you just log an object to the console. If you"re actually trying to do something with the data, you should pull out only the things you need from this object and put them into a new trimmed down object that you can pass to JSON.stringify. This object looks like it came from a Google API and has lots of extra data in it.

If you don"t mind destructively modifying the object, try selectively nulling out suspicious fields and see if JSON.stringify will accept the object. At least that way you"ll know what"s causing it. Note that if you do this you may end up breaking the object for any future uses.

以上

 Thank you!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/78638.html

相关文章

  • Memory Leak in JavaScript

    Memory Leak (內存泄漏) Origin: https://aleen42.gitbooks.io/p...Date: Sep, 10th, 2016 In the process of developing front-end application, like web application, memory leak means that a space created before...

    LeviDing 评论0 收藏0
  • 简洁易用的基于Promise的jsonp库easy-jsonp,了解下

    现在流行的axios库不支持jsonp,因此这里推荐一个jsonp库,简单易用。以下是介绍: Easy JSONP A minimal and lightweight JSONP implementation which is used to be a kind of cross domain solutions. Features Implement JSONP request from th...

    Dr_Noooo 评论0 收藏0
  • Learning Notes - Understanding the Weird Parts of

    摘要:标签前端作者更多文章个人网站 Learning Notes - Understanding the Weird Parts of JavaScript 标签 : 前端 JavaScript [TOC] The learning notes of the MOOC JavaScript: Understanding the Weird Parts on Udemy,including...

    inapt 评论0 收藏0
  • Licia:最全最实用的 JavaScript 工具库

    摘要:为了避免不同项目之间进行复制粘贴,可以将这些常用的函数封装到一起并发布包。目前所包含模块已达三百个,基本可以满足前端的日常工发需求。二使用打包工具该项目自带打包工具,可以通过配置文件或命令行扫描源码自动生成项目专用的工具库。 前言 在业务开发过程中,我们经常会重复使用日期格式化、cookie 操作、模板、浏览器判断、类型判断等功能。为了避免不同项目之间进行复制粘贴,可以将这些常用的函数...

    luxixing 评论0 收藏0
  • jQuery 源码系列(十九)ajax 的相关操作

    摘要:欢迎来我的专栏查看系列文章。主要来看函数函数返回值有两个,其中就是发送函数了,一步一步,发送下来,无需多说明。参考源码分析系列整体结构源码分析系列总结触碰异步详解本文在上的源码地址,欢迎来。 欢迎来我的专栏查看系列文章。 关于 ajax,东西太多了,我本来想避开 ajax,避而不提,但觉得 ajax 这么多内容,不说又少点什么,就简单点来介绍吧,加上最近准备内推面试的时候,看了不少 a...

    Dean 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<