我正在尝试将格式化的json字符串添加到我的textarea字段中。但它不起作用。
我正在使用这种方法格式化字符串:How can I beautify JSON programmatically?
然后只需致电
textarea.setValue(formattedJson);
检查结果快照:
在textarea中获取格式正确的字符串是否合适?
答案 0 :(得分:9)
JSON.stringify
方法将对象作为参数。您可能会发送字符串作为参数。这是正确的代码:
jsonObj = {a: 'b', c: [1,2,3]};
// note that jsonObj is object, not string
jsonString = JSON.stringify(jsonObj, null, '\t');
textarea.setValue(jsonString);
这是jsfiddle