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
Add a ment  | 

1 Answer 1

Reset to default 9

this 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