JSON (JavaScript Object Notation)为一种数据交换结构,相比 XML,JSON 的语法显得更为轻巧,能够大大地减少数据交换时所要传递的数据量。现在许多网络服务的 API (Application Programming Interface)是使用 JSON 格式的文字消息来进行数据交换。绝大部分的企业提供的 API 所传回的 JSON 消息,没有使用空格字符或是换行字符来进行排版,为的就是要节省数据传输量,反正这些消息主要是给机器看的又不是人看的。但如果是开发者,就常会需要研究这些从服务器传回来的消息,为了要让 JSON 消息更加容易阅读,势必得对其进行增加空格、换行与缩进的处理。

MsonFrame 是一个 JavaFX 的应用程序,它使用 Mson 来将 JSON 格式的文字消息转成 XML 格式,也可以将 XML 格式的文字消息转成 JSON 格式。同时,它也会对 JSON 文字消息进行排版的处理,让消息看起来更直觉、美观。

MsonFrame GitHub 项目:

使用 MsonFrame

MsonFrame 的使用方法十分简单,只要在文字方块内输入文字即可。

例如输入一个 JSONObject 的文字消息:

{"website":"http://magiclen.org","authors":[{"birthday":[1993,8,10],"phone":[88693929304,"041112233"],"name":"Magic Len","id":1,"lang":"繁体中文"},{"birthday":[1991,1,1],"name":"Dean ML","phones":["711423000","556432234"],"id":2,"lang":"English"}]}

结果为:

{
    "website" : "http://magiclen.org",
    "authors" : [
        {
            "birthday" : [
                1993,
                8,
                10
            ],
            "phone" : [
                88693929304,
                "041112233"
            ],
            "name" : "Magic Len",
            "id" : 1,
            "lang" : "繁体中文"
        },
        {
            "birthday" : [
                1991,
                1,
                1
            ],
            "name" : "Dean ML",
            "phones" : [
                "711423000",
                "556432234"
            ],
            "id" : 2,
            "lang" : "English"
        }
    ]
}

----------------

<website>http://magiclen.org</website><authors><birthday>1993</birthday><birthday>8</birthday><birthday>10</birthday><phone>88693929304</phone><phone>041112233</phone><name>Magic Len</name><id>1</id><lang>繁体中文</lang></authors><authors><birthday>1991</birthday><birthday>1</birthday><birthday>1</birthday><name>Dean ML</name><phones>711423000</phones><phones>556432234</phones><id>2</id><lang>English</lang></authors>

MsonFrame 转换 JSONObject 的结果

卷动后的 JSONObject 转换结果

例如输入一个 JSONArray 的文字消息:

[{"a":5}, 123, 123.333, true, false, "string"]

结果为:

[
	{
		"a" : 5
	},
	123,
	123.333,
	true,
	false,
	"string"
]

----------------

<array><a>5</a></array><array>123</array><array>123.333</array><array>true</array><array>false</array><array>string</array>

MsonFrame 转换 JSONArray 的结果

例如输入一个 XML 格式的文字消息:

<html lang="zh-TW">
	<head>
		<meta charset="UTF-8" />
		<title>
			MagicLen
		</title>
	</head>
	<body>
		<div id="container">
			Welcome my website!
		</div>
	</body>
</html>

结果为:

{
	"root" : {
		"html" : {
			"head" : {
				"meta" : {
					"charset" : "UTF-8"
				},
				"title" : "MagicLen"
			},
			"lang" : "zh-TW",
			"body" : {
				"div" : {
					"id" : "container",
					"content" : "Welcome my website!"
				}
			}
		}
	}
}

----------------

<root><html><head><meta><charset>UTF-8</charset></meta><title>MagicLen</title></head><lang>zh-TW</lang><body><div><id>container</id>Welcome my website!</div></body></html></root>

MsonFrame 转换 XML 的结果