admin管理员组文章数量:1309939
Is there any way to convert javascript object into JSON.
I can not use
JSON.stringify(<obj>)
Because there is no stringify method in the JSON object in the following link.
Link
Example:
var obj = {'x':1,'y':2}
Now if I'll run
console.log(JSON.stringify(obj));
Then I'm getting this error.
error: TypeError: Object # has no method 'stringify'
Is there any way to convert javascript object into JSON.
I can not use
JSON.stringify(<obj>)
Because there is no stringify method in the JSON object in the following link.
Link
Example:
var obj = {'x':1,'y':2}
Now if I'll run
console.log(JSON.stringify(obj));
Then I'm getting this error.
Share Improve this question asked Mar 25, 2014 at 8:46 Dixit SinglaDixit Singla 2,6303 gold badges27 silver badges43 bronze badges 2error: TypeError: Object # has no method 'stringify'
- No repo, jsfiddle/nXQxC – Praveen Commented Mar 25, 2014 at 8:48
- Fiddle works fine in Chrome – Starscream1984 Commented Mar 25, 2014 at 8:53
4 Answers
Reset to default 2Well, the website overrides the actual JSON object with their own.
> JSON
> Object {toStr: function, quote: function}
Try using JSON.toStr(object)
It appears that some code/library has overridden the global JSON object. JSON.toStr
is fine but if you want the original JSON
objet back you can always create an invisible frame and use its global objects
OriginalJSON = (function() {
var e = document.createElement('frame');
e.style.display = 'none';
var f = document.body.appendChild(e);
return f.contentWindow.JSON;
})()
OriginalJSON.stringify({a: 1})
This is a technique that works for all global objects that has been overridden for some reason. As an alternative you can always copy only the stringify
method
JSON.stringify = (function() {
var e = document.createElement('frame');
e.style.display = 'none';
var f = document.body.appendChild(e);
return f.contentWindow.JSON.stringify;
})()
// Now JSON.stringify is back
JSON.stringify({a: 1})
stringify
method depends on your browser.
So if you cannot find JSON.stringify()
, maybe the browser you're using is not patible to JSON API, you could include this library to make it there:
json2.js
Consider using external library, for example JSON 3 is a good choice.
本文标签: How to convert javascript object into json without using quotJSONstringifyquot methodStack Overflow
版权声明:本文标题:How to convert javascript object into json without using "JSON.stringify" method? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741860196a2401578.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论