我有一个页面,我想让用户编辑一个应用程序 - 存储在JSON中。它看起来像这样:
{"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 pattern = ",", re = new RegExp(pattern, "g");
casedetails.innerHTML = JSON.stringify(apps[0]).replace(re , ", ");
问题是在我进行更改后,第一次运行此操作时,textarea不显示换行符 - 即使它们在那里! (如果我复制/粘贴它们会出现)。如果我刷新它会显示换行符。
任何人都可以帮我弄清楚如何在新线上获取JSON而无需刷新?我正在使用IE 9
谢谢!
- 汤姆
答案 0 :(得分:4)
这应该很简单...... 看看这个jsFiddle是否符合您的需求
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。我使用jQuery只是为了加速示例,但你应该关注的功能是JSON.stringify