如何在Lua中将包含多个元素和子元素的字符串拆分为表
我正在编写一个测试函数,可以将字符串解析为表。以下是测试字符串:如何在Lua中将包含多个元素和子元素的字符串拆分为表,lua,Lua,我正在编写一个测试函数,可以将字符串解析为表。以下是测试字符串: {"part A": [{"id":2264,"name":"My Tank","uniqueId":"014682003358115"}],"Part B" :[{"id":32,"attributes":{"adc":"0","raw":"0","cid":"89302750000000044889","distance":6277.98,"land":"SE-8-45-3-W2","ip":"193.181.246.215"
{"part A": [{"id":2264,"name":"My Tank","uniqueId":"014682003358115"}],"Part B" :[{"id":32,"attributes":{"adc":"0","raw":"0","cid":"89302750000000044889","distance":6277.98,"land":"SE-8-45-3-W2","ip":"193.181.246.215"},"deviceId":2264,
"portName":"demo test ca","portNumber":1}]}
我想要的结果是这样的:
Part A:
id:2264
name: "My tank"
....etc
Part B:
id: 32
attribute: (here is a sub table where element is belong to attribute)
adc: 0
.....etc
function ParseString(s, delimiter)
local result = {}
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match)
end
return result
end
我在lua文档中寻找分割联接,但它没有回答如何放置表和子表
我想到的是这样简单的事情:
Part A:
id:2264
name: "My tank"
....etc
Part B:
id: 32
attribute: (here is a sub table where element is belong to attribute)
adc: 0
.....etc
function ParseString(s, delimiter)
local result = {}
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match)
end
return result
end
但是它不适用于这个字符串中的多个delimeterSee,我会使用一个经过良好测试的JSON模块,比如See,我会使用一个经过良好测试的JSON模块,比如