如何在Lua中将具有多个元素和子元素的字符串拆分为表

时间:2019-04-29 21:13:31

标签: 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"},"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

我在lua文档中寻找了拆分联接,但是它并没有回答如何放置表和子表。

我在想这样简单的事情:

function ParseString(s, delimiter)
    local result = {}
    for match in (s..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match)

    end

    return result

end

但不适用于此字符串中的多个分度符

0 个答案:

没有答案