admin 管理员组文章数量: 1086019
This is going to sound really ghetto, but I need to print some Javascript to the browser screen so that it can be cut and pasted into another program.
I'm using JSON.stringify()
from json2.js
, however, its not escaping characters such as quotes and new lines (",\n) which are actually control parts of a JSON object and need to be escaped.
For example, I'd get strings like this that cause problems when importing into the other program:
{
string_property_01 : "He said "HI""; // The string terminates after "He said "
}
Are there any libraries that I can use to escape all the characters that I need to escape here?
Thanks!
This is going to sound really ghetto, but I need to print some Javascript to the browser screen so that it can be cut and pasted into another program.
I'm using JSON.stringify()
from json2.js
, however, its not escaping characters such as quotes and new lines (",\n) which are actually control parts of a JSON object and need to be escaped.
For example, I'd get strings like this that cause problems when importing into the other program:
{
string_property_01 : "He said "HI""; // The string terminates after "He said "
}
Are there any libraries that I can use to escape all the characters that I need to escape here?
Thanks!
Share Improve this question asked Jul 14, 2011 at 1:08 Chris DutrowChris Dutrow 50.5k67 gold badges196 silver badges262 bronze badges 1- How do you print it to the screen? – Dr.Molle Commented Jul 14, 2011 at 1:31
3 Answers
Reset to default 5Option #2
var g = {
sampleFunc2 : function (data) {
var dataAsText = JSON.stringify(data);
// w jquery
$('#debugArea').text(dataAsText);
}
}
// usage...
g.sampleFunc2({ id: "4", name: "John Smith" });
In markup:
<textarea id='debugArea' rows='10' cols='50'></textarea>
I do the following, its a beautiful hack.
var g = {
sampleFunc : function (data) {
var dataAsText = JSON.stringify(data);
var response = prompt('you can copy and paste this', dataAsText);
}
}
// usage...
g.sampleFunc({ id: "4", name: "John Smith" });
JavaScript prompt... gotta love it.
Are you sure this isn't just a browser rendering thing playing tricks on you? A JSON library is going to escape chars properly to give you a valid JSON string. Have you tried paring the output of json2.js to the native JSON.stringify some browsers (like chrome) have?
本文标签: javascriptPrint JSON to screen for use with cut and pasteStack Overflow
版权声明:本文标题:javascript - Print JSON to screen for use with cut and paste - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744045912a2524111.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论