Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Lua中将包含多个元素和子元素的字符串拆分为表_Lua - Fatal编程技术网

如何在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

但是它不适用于这个字符串中的多个delimeter

See,我会使用一个经过良好测试的JSON模块,比如See,我会使用一个经过良好测试的JSON模块,比如