1

在将 Json String 发布到 StackOverflow 之前,格式化 Json String 的最简单方法是什么?

我不想手动格式化它,大多数在线网站只验证 Json 但不格式化。

从:

[{"lines":{"0": "Hammersmith & City","1": "Circle"},"id": "233","name": "Shepherd's Bush Market"},{"lines": {"0": "Hammersmith & City","1": "Circle"},"id": "233","name": "Shepherd's Bush Market"},{"lines": {"0": "Hammersmith & City","1": "Circle"},"id": "233","name": "Shepherd's Bush Market"}]

我想让它像:

    [{
    "lines": {
        "0": "Hammersmith & City",
        "1": "Circle"
    },
    "id": "233",
    "name": "Shepherd's Bush Market"
},
{
    "lines": {
        "0": "Hammersmith & City",
        "1": "Circle"
    },
    "id": "233",
    "name": "Shepherd's Bush Market"
},
{
    "lines": {
        "0": "Hammersmith & City",
        "1": "Circle"
    },
    "id": "233",
    "name": "Shepherd's Bush Market"
}]

谢谢,

[编辑]

没有对 java 的引用,但这个问题对于 java(Android) 开发人员来说是实际的

4

4 回答 4

3

我看到很多基于Json解析的问题。

用户按原样发布 Json 的一些问题:

或在一行中,例如:

[{"lines":{"0": "Hammersmith & City","1": "Circle"},"id": "233","name": "Shepherd's Bush Market"},{"lines": {"0": "Hammersmith & City","1": "Circle"},"id": "233","name": "Shepherd's Bush Market"},{"lines": {"0": "Hammersmith & City","1": "Circle"},"id": "233","name": "Shepherd's Bush Market"}]

或尝试手动格式化 Json 字符串,如:

[{"lines":{"0": "Hammersmith & City","1": "Circle"},
"id": "233","name": "Shepherd's Bush Market"},
{"lines": {"0": "Hammersmith & City","1": "Circle"},
"id": "233","name": "Shepherd's Bush Market"},
{"lines": {"0": "Hammersmith & City","1": "Circle"},
"id": "233","name": "Shepherd's Bush Market"}]

可悲的是,当用户仅因为 Json 字符串格式不正确并且他不知道它是数组还是对象而无法解决问题时。

无论如何,有一种简单的方法可以使用Notepad++

  • 打开记事本++
  • 打开插件管理器,如:

在此处输入图像描述

  • 在“可用”选项卡中选择JSON viewer

在此处输入图像描述

安装它并重新启动 Notapad++

完成后,您可以格式化 Json,例如:

在此处输入图像描述

现在您已经格式化了 Json,并准备将其发布到您的问题中。

[{
    "lines": {
        "0": "Hammersmith & City",
        "1": "Circle"
    },
    "id": "233",
    "name": "Shepherd's Bush Market"
},
{
    "lines": {
        "0": "Hammersmith & City",
        "1": "Circle"
    },
    "id": "233",
    "name": "Shepherd's Bush Market"
},
{
    "lines": {
        "0": "Hammersmith & City",
        "1": "Circle"
    },
    "id": "233",
    "name": "Shepherd's Bush Market"
}]
于 2013-10-04T08:24:22.497 回答
1

谷歌浏览器有一个非常好的扩展名为JSON Formatter,它可以将您json string的格式设置为实际json格式。它还告诉您的JSON字符串中是否有任何错误。

这是链接

于 2013-10-04T08:39:57.487 回答
1

JSONEditorOnline完美地做到了这一点。您必须确保您的 JSON 有效,但网站会在无效时告诉您(是的,其他一些格式化程序网站在不告诉您为什么JSON 无效时无法工作)

于 2013-10-04T08:42:27.580 回答
0

您可以尝试使用Formatter JSON,这是一个强大的工具,可以验证并制作漂亮的 JSON。

于 2017-03-28T16:05:01.613 回答