有没有一种快速的方法可以在文本编辑器中将 JavaScript 对象转换为有效的 JSON?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5810635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:00:16  来源:igfitidea点击:

Is there a quick way to convert a JavaScript object to valid JSON in the text editor?

javascriptjsontext-editor

提问by No Surprises

I have a big old config object. Something like:

我有一个很大的旧配置对象。就像是:

var object = {
  item1: 'value1',
  item2: 1000,
  item3: ['a', 'b', 'c'],
  item4: [1, 2, 3],
  item5: {
    foo: 'bar'
  }
};

... and so on. I want to rewrite it as valid JSON so it can travel through the intertubes, but I don't want to go through every line in my file manually adding double quotes all over the place. Of course, I don't mind manually wrapping the whole thing in brackets and changing the initial assignment to be the first property, but beyond that I was hoping there's some resource that will do the grunt work.

... 等等。我想将它重写为有效的 JSON,以便它可以通过 intertubes,但我不想手动遍历文件中的每一行,并在各处手动添加双引号。当然,我不介意手动将整个内容括在括号中并将初始分配更改为第一个属性,但除此之外,我希望有一些资源可以完成繁重的工作。

Anyway, please help me out if know of a TextMate command, regex trick, online converter, friendly robot, or anything else that will make this less tedious.

无论如何,如果知道 TextMate 命令、正则表达式技巧、在线转换器、友好的机器人或其他任何可以使这变得不那么乏味的东西,请帮助我。

回答by Phrogz

  1. Launch Firefox/Chrome/Safari
  2. Open Firebug/developer tools
  3. Copy/paste your code into the console.
  4. Then type console.log(JSON.stringify(object))and voila!

    {"item1":"value1","item2":1000,"item3":["a","b","c"],
     "item4":[1,2,3],"item5":{"foo":"bar"}}
    
  5. Copy/paste back into your text editor.
  1. 启动 Firefox/Chrome/Safari
  2. 打开 Firebug/开发人员工具
  3. 将您的代码复制/粘贴到控制台中。
  4. 然后打字console.log(JSON.stringify(object)),瞧!

    {"item1":"value1","item2":1000,"item3":["a","b","c"],
     "item4":[1,2,3],"item5":{"foo":"bar"}}
    
  5. 复制/粘贴回您的文本编辑器。


For more control over the formatting, I have a free online webpage:

为了更好地控制格式,我有一个免费的在线网页:

http://phrogz.net/JS/NeatJSON

http://phrogz.net/JS/NeatJSON

that lets you paste JSON or JS values in one box and see JSON at the bottom, with lots of knobs and sliders to adjust how it looks. For example, the JS value ["foo","bar",{dogs:42,piggies:0,cats:7},{jimmy:[1,2,3,4,5],jammy:3.14159265358979,hot:"pajammy"}]can be formatted like any of the following (and more):

它可以让你将 JSON 或 JS 值粘贴到一个框中,并在底部看到 JSON,有很多旋钮和滑块可以调整它的外观。例如,JS 值["foo","bar",{dogs:42,piggies:0,cats:7},{jimmy:[1,2,3,4,5],jammy:3.14159265358979,hot:"pajammy"}]可以格式化为以下任何一种(以及更多):

[
    "foo",                            <- adjustable indentation
    "bar",
    {"dogs":42,"piggies":0,"cats":7}, <- small objects on one line!
    {
        "jimmy":[1,2,3,4,5],          <- small arrays on one line!
        "jammy":3.142,                <- decimal precision!
        "hot":"pajammy"
    }
]
[
  "foo",
  "bar",
  { "cats":7, "dogs":42, "piggies":0 }, <- spaces inside braces!
  {
    "hot":"pajammy",                    <- sort object keys!
    "jammy":3.14159265358979,
    "jimmy":[ 1, 2, 3, 4, 5 ]           <- spaces after commas!
  }
]
[ "foo",                           <- 'short' format puts first value
  "bar",                           <- on same line as opening bracket...
  { "dogs"    : 42,
    "piggies" : 0,                 
    "cats"    : 7 },               <- ...and close bracket with last value!
  { "jimmy" : [ 1, 2, 3, 4, 5 ],
    "jammy" : 3.14159265358979,    <- spaces around colons!
    "hot"   : "pajammy" } ]        <- align object values!

Screenshot of NeatJSON webpage

NeatJSON 网页截图

回答by Cheeso

Why wouldn't you just....

你为什么不只是......

...send the result of JSON.stringify(). You don't need to type inthe JSON, you need to generate it at runtime if I am not mistaken, so...

...发送 JSON.stringify() 的结果。你不需要输入JSON,如果我没记错的话,你需要在运行时生成它,所以......

var mything = { .... } ; 
var jsonRep = JSON.stringify(mything); 

See also, Serializing an object to JSON

另请参阅将对象序列化为 JSON

回答by Felix

You can use Google Chrome's console (or Firebug, probably):

您可以使用 Google Chrome 的控制台(或 Firebug,可能):

> object
  Object
    item1: "value1"
    item2: 1000
    item3: Array[3]
    item4: Array[3]
    item5: Object
    __proto__: Object
> JSON.stringify(object);
"{"item1":"value1","item2":1000,"item3":["a","b","c"],"item4":[1,2,3],"item5":{"foo":"bar"}}"

If you want a bit of further documentation, check out Using native JSONon the MDC.

如果您需要更多文档,请查看在 MDC 上使用本机 JSON

回答by faboulaws

Another JS to JSONonline conversion tool with syntax highlighting. Javascript to JSON converter

另一个带有语法高亮的JS 到 JSON在线转换工具。 Javascript 到 JSON 转换器

回答by dominik

I made a tool for this: bit.ly/js2json. It takes JavaScript and outputs JSON.

我为此制作了一个工具:bit.ly/js2json。它需要 JavaScript 并输出 JSON。