json.prettify()

类型 函数
json.*
返回值 字符串
修订版 2024.3703 版
关键字 json
另请参阅 json.encode()

概述

返回一个字符串,它是指定对象的具有人类可读性的表示形式(或有效的 JSON 字符串)。该字符串已缩进,并且顶层键已按字母顺序排列,适合与 print() 一起显示在控制台中。

语法

json.prettify( obj )
obj (必需)

字符串. 包含 JSON 数据的 Lua 表或字符串。

示例

local json = require "json"

local t = { ["item1"] = "value1", ["pi"] = 3.1415,
    ["name2"] = { 1, false, true, 23.54, "a \021 string" },
}

print( "t: " .. json.prettify( t ) )
--> t: {
-->   "item1":"value1",
-->   "name2":[1,false,true,23.54,"a \u0015 string"],
-->   "pi":3.1415
--> }

local line = display.newLine( 200, 90, 227, 165 )

print("line._properties: " .. json.prettify( line._properties ) )
--> line._properties: {
-->   "alpha":1,
-->   "anchorSegments":"false",
-->   "anchorX":0.5,
-->   "anchorY":0.5,
-->   "blendMode":"normal",
-->   "contentBounds":{
-->     "xMin":198,
-->     "xMax":229,
-->     "yMax":167,
-->     "yMin":88
-->   },
-->   "contentHeight":79,
-->   "contentWidth":31,
-->   "height":79,
-->   "isHitTestMasked":"false",
-->   "isHitTestable":"false",
-->   "isVisible":"true",
-->   "maskRotation":0,
-->   "maskScaleX":0,
-->   "maskScaleY":0,
-->   "maskX":0,
-->   "maskY":0,
-->   "rotation":0,
-->   "stroke":{
-->     "blendMode":"normal",
-->     "a":1,
-->     "r":1,
-->     "blendEquation":"add",
-->     "effect":"nil",
-->     "g":1,
-->     "b":1
-->   },
-->   "strokeWidth":1,
-->   "width":31,
-->   "x":200,
-->   "xScale":1,
-->   "y":90,
-->   "yScale":1
--> }