No *_*ses 36 javascript json text-editor
我有一个很大的旧配置对象.就像是:
var object = {
item1: 'value1',
item2: 1000,
item3: ['a', 'b', 'c'],
item4: [1, 2, 3],
item5: {
foo: 'bar'
}
};
Run Code Online (Sandbox Code Playgroud)
... 等等.我想重写为有效的JSON,因此它可以通过intertubes旅行,但我并不想通过每一行中我的文件到处都是手动添加双引号.当然,我不介意手动包装在括号中的整个事情,改变了最初的任务是第一属性,但除此之外,我希望有一些资源,将做繁重的工作.
无论如何,如果知道TextMate命令,正则表达式技巧,在线转换器,友好机器人或任何其他会使这不那么繁琐的事情,请帮助我.
Phr*_*ogz 120
然后键入console.log(JSON.stringify(object))并瞧!
{"item1":"value1","item2":1000,"item3":["a","b","c"],
"item4":[1,2,3],"item5":{"foo":"bar"}}
Run Code Online (Sandbox Code Playgroud)为了更好地控制格式,我有一个免费的在线网页:
它允许您将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"
}
]
Run Code Online (Sandbox Code Playgroud)
[
"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!
}
]
Run Code Online (Sandbox Code Playgroud)
[ "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!
Run Code Online (Sandbox Code Playgroud)
你为什么不......
...发送JSON.stringify()的结果.你不需要输入 JSON,如果我没弄错的话你需要在运行时生成它,所以...
var mything = { .... } ;
var jsonRep = JSON.stringify(mything);
Run Code Online (Sandbox Code Playgroud)
另请参见将对象序列化为JSON
| 归档时间: |
|
| 查看次数: |
30970 次 |
| 最近记录: |