在网页上的文本框中格式化json数据的最简单方法是什么?

时间:2023-01-27 16:42:26

I am currently using edit_area_full.js on a few web pages to format python code examples. I have a few cases where I want to pretty format json data, but edit_area does not support json. What is the best drop in replacement for edit_area that can format json data in a text box in a webpage?

我目前在几个网页上使用edit_area_full.js来格式化python代码示例。我有几个案例,我想格式化json数据,但edit_area不支持json。什么是替换edit_area的最佳下降,可以在网页的文本框中格式化json数据?

Here is how I am formatting python code.

这是我如何格式化python代码。

<p><textarea id="code-area" name="user_code" rows="12" cols="80">if True: pass</textarea></p>

<script language="javascript" type="text/javascript" src="/static/edit_area_full.js"></script>
  <script language="javascript" type="text/javascript">
    editAreaLoader.init({
      id: "code-area",
      syntax: "python",
      start_highlight: true,
      allow_resize: "no",
      font_size: 12,
      toolbar: "new_document, |, help",
      replace_tab_by_spaces: 4
    });
  </script>

1 个解决方案

#1


1  

You can do it using:

你可以使用:

  • Edit_area_full.js with 'syntax' = 'Javascript'. This will make all the nice JSON highlighting.
  • 使用'syntax'='Javascript'编辑Edit_area_full.js。这将使所有漂亮的JSON突出显示。

  • To reformat JSON you can use this library - http://github.com/einars/js-beautify
  • 要重新格式化JSON,您可以使用此库 - http://github.com/einars/js-beautify

You can test the later library in work here: http://jsbeautifier.org/

你可以在这里测试后面的库:http://jsbeautifier.org/

For example, I fed it this:

例如,我喂它:

var testvar = {asd:[abc,cde,fg,{hello: world},1,2,3],ccc:"aaa",ddd:33,cas:[qwerty,[1,[2],3],4]};

And got this in result:

得到这个结果:

var testvar = {
    asd: [abc, cde, fg, {
        hello: world
    }, 1, 2, 3],
    ccc: "aaa",
    ddd: 33,
    cas: [qwerty, [1, [2], 3], 4]
};

#1


1  

You can do it using:

你可以使用:

  • Edit_area_full.js with 'syntax' = 'Javascript'. This will make all the nice JSON highlighting.
  • 使用'syntax'='Javascript'编辑Edit_area_full.js。这将使所有漂亮的JSON突出显示。

  • To reformat JSON you can use this library - http://github.com/einars/js-beautify
  • 要重新格式化JSON,您可以使用此库 - http://github.com/einars/js-beautify

You can test the later library in work here: http://jsbeautifier.org/

你可以在这里测试后面的库:http://jsbeautifier.org/

For example, I fed it this:

例如,我喂它:

var testvar = {asd:[abc,cde,fg,{hello: world},1,2,3],ccc:"aaa",ddd:33,cas:[qwerty,[1,[2],3],4]};

And got this in result:

得到这个结果:

var testvar = {
    asd: [abc, cde, fg, {
        hello: world
    }, 1, 2, 3],
    ccc: "aaa",
    ddd: 33,
    cas: [qwerty, [1, [2], 3], 4]
};