admin管理员组文章数量:1287598
I have a page where I want to let the user edit an application - stored in a JSON. It looks like this:
{"address":{"House_Number":505,
"Street_Direction":"",
"Street_Name":"Claremont",
"Street_Type":"Street",
"Apt":"15L",
"Burough":"Brooklyn",
"State":"NY",
"Zip":"10451",
"Phone":"718-777-7777"},
"casehead":0,
"adults":[{"Last_Name":"Foo",
"First_Name":"A",
"Sex":"M",
"Date_Of_Birth":"01011980"}],
"children":[]}
I am setting this using
var pattern = ",", re = new RegExp(pattern, "g");
casedetails.innerHTML = JSON.stringify(apps[0]).replace(re , ", ");
The problem is after I make changes, the first time I run this, the textarea doesn't show the newlines - even though they are there! (If I copy/paste they show up). If I refresh it shows the newlines.
Can anyone help me figure out how to get JSON on new lines without having to refresh? I am using IE 9
Thanks!
-Tom
I have a page where I want to let the user edit an application - stored in a JSON. It looks like this:
{"address":{"House_Number":505,
"Street_Direction":"",
"Street_Name":"Claremont",
"Street_Type":"Street",
"Apt":"15L",
"Burough":"Brooklyn",
"State":"NY",
"Zip":"10451",
"Phone":"718-777-7777"},
"casehead":0,
"adults":[{"Last_Name":"Foo",
"First_Name":"A",
"Sex":"M",
"Date_Of_Birth":"01011980"}],
"children":[]}
I am setting this using
var pattern = ",", re = new RegExp(pattern, "g");
casedetails.innerHTML = JSON.stringify(apps[0]).replace(re , ", ");
The problem is after I make changes, the first time I run this, the textarea doesn't show the newlines - even though they are there! (If I copy/paste they show up). If I refresh it shows the newlines.
Can anyone help me figure out how to get JSON on new lines without having to refresh? I am using IE 9
Thanks!
-Tom
Share Improve this question asked Mar 18, 2015 at 17:22 ControlAltDelControlAltDel 35.1k10 gold badges58 silver badges83 bronze badges 3-
Check out the documentation of
JSON.stringify
. – Felix Kling Commented Mar 18, 2015 at 17:27 - can't reproduce -> jsfiddle/heL4kkj7 ? – adeneo Commented Mar 18, 2015 at 17:29
- @adeneo sadly no. In the fiddle my entries are still not on new lines – ControlAltDel Commented Mar 18, 2015 at 17:55
1 Answer
Reset to default 9this should be pretty simple... take a look if this jsFiddle fit your needs
var yourObject = {"address":{"House_Number":505,
"Street_Direction":"",
"Street_Name":"Claremont",
"Street_Type":"Street",
"Apt":"15L",
"Burough":"Brooklyn",
"State":"NY",
"Zip":"10451",
"Phone":"718-777-7777"},
"casehead":0,
"adults":[{"Last_Name":"Foo",
"First_Name":"A",
"Sex":"M",
"Date_Of_Birth":"01011980"}],
"children":[]};
var textedJson = JSON.stringify(yourObject,undefined, 2);
document.getElementById("json-area").value = textedJson
;
P.S. i've used jQuery just to speed-up the example but the function on which you should focus is JSON.stringify
本文标签: javascriptFormatting JSON in a textareaStack Overflow
版权声明:本文标题:javascript - Formatting JSON in a textarea - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741245667a2364824.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论