有没有特别注意简洁的 JSON 漂亮打印机?
我想要一个漂亮的 JSON 打印机,它可以识别数组或对象何时适合一行并执行此操作。示例:
{
"fits": ["JSON", "pretty", "printer"],
"longer": [
"???????????????????????????????????????????????????",
"???????????????????????????????????????????????????",
"???????????????????????????????????????????????????",
"???????????????????????????????????????????????????",
"???????????????????????????????????????????????????"
]
}
有这样的独立库吗?如果没有,我该如何去写呢?
我对 JavaScript 实现最感兴趣。
I'd like a JSON pretty printer that would recognize when an array or object fits on one line and just do that. Example:
{
"fits": ["JSON", "pretty", "printer"],
"longer": [
"???????????????????????????????????????????????????",
"???????????????????????????????????????????????????",
"???????????????????????????????????????????????????",
"???????????????????????????????????????????????????",
"???????????????????????????????????????????????????"
]
}
Is there a standalone library like this? If not, how would I go about writing one?
I'm most interested in a JavaScript implementation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道有没有这样简洁的 JSON 打印机,但如果您愿意的话,制作自己的打印机应该不难:
for(property in object)
来迭代属性给定对象的。hasOwnProperty
进行过滤。I don't know about any such concise JSON printer, but it shouldn't be hard to make your own if you want to:
for(property in object)
to iterate over the properties of a given object.hasOwnProperty
.typeof
由于 JSON 主要是一种数据传输格式,我假设您的意思是在浏览器中查看原始 JSON?如果是这样,那么有几个选项:
你应该能够挖掘如果您需要进一步定制,请进入最后三个的源代码。我首先迭代
value.length
属性,其中value
是数组元素,看看是否可以将输出限制为单行。Since JSON is primarily a data transport format, I assume that you mean viewing raw JSON in the browser? If so, then there are a few options:
You should be able to dig into the source of the last three if you require further customization. I'd start with iterating through the
value.length
property wherevalue
is/are the array element(s) to see if you can limit your output to a single line.使用替换函数来比较总数每个键/值对中的字符数为固定长度。这是一个简单的例子:
用法:
Use a replacer function to compare the total number of characters in each key/value pair to a fixed length. Here is a simple example:
Usage: