解码并解析JSON到Lua [英] Decode and Parse JSON to Lua

查看:111
本文介绍了解码并解析JSON到Lua的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下要转换为Lua的JSON数据,以访问每个publish_topic和sample_rate值.

  {"00-06-77-2f-37-94":{"publish_topic":"/stations/test","sample_rate":5000}} 

如果我理解正确,Lua表将如下所示:

  {00-06-77-2f-37-94 ="publish_topic":"/stations/test","sample_rate":5000} 

接下来,我将遍历表格以将每个值保存到本地变量中.

但是,如果我尝试打印表的值(使用以下代码),则返回"nil".读取表值的代码是否错误?该表是否具有两个值或仅是一个值:["publish_topic":"/stations/test","sample_rate":5000]?

  lua_value = JSON:解码(数据)为_,d成对(lua_value)做打印(lua_value [d])结尾本地主题= lua_value [0]本地计时器= lua_value [1]结尾 

我正在Lua使用以下JSON库: http://regex.info/blog/lua/json

Edit2:@Piglet:我实现了您的脚本并通过添加一个表 (conversionTable) 对其进行了修改,其中两个元素publish_topic":/stations/test"和"sample_rate:5000"将分别保存在变量pubtop和rate中.但是,当我同时打印两个变量时,在两种情况下都返回nil.如何从该表中提取信息以保存在变量中?

最终我实际上只想将值"/stations/test"保存到和"5000"这些变量.我是否需要解析上面的每个元素才能获得这些元素?还是有另一种方法?

 本地pubtop当地利率局部函数printTable(t)本地转换表= {}对于k,v成对(t)做如果type(v)=="table"然后conversionTable [k] = string.format(%q:{",k)printTable(v)打印(}")别的print(string.format(%q:",k).. v ..,")结尾结尾pubtop = conversionTable [0]汇率= conversionTable [1]结尾局部lua_value局部函数handleOnReceive(topic,data,_,_)print("handleOnReceive:主题'..主题..'消息'''..数据.."")-此样本将收到的消息发布到test/topic2打印(数据)lua_value = JSON:解码(数据)printTable(lua_value)打印(桌面)打印(率)结尾client:register('OnReceive',handleOnReceive) 

解决方案

我不知道您使用的是哪个json库,因此我无法告诉您JSON:decode(data)是正确的方法.

假设lua_value像这样:

  local lua_value = {["00-06-77-2f-37-94"] = {publish_topic ="/stations/test",采样率= 5000}} 

然后你的循环

成对的_,d的

 (lua_value)做打印(lua_value [d])结尾 

确实会打印 nil .

lua_value 在表的键"00-06-77-2f-37-94" 中具有单个元素.

每个循环迭代将为您提供一个键值对.所以 d 实际上是值,因此是 lua_value

的内部表

所以您实际上是在这样做:

  local innerTable = lua_value ["00-06-77-2f-37-94"]打印(lua_value [innerTable]) 

当然 lua_value [innerTable] nil .

尝试类似

 函数printTable(t)对于k,v成对(t)做如果type(v)=="table"然后print(string.format(%q:{",k))printTable(v)打印(}")别的print(string.format(%q:",k).. v ..,")结尾结尾结尾printTable(lua_value) 

I have following JSON data I would like to decode to Lua to access each the publish_topic and sample_rate value.

{"00-06-77-2f-37-94":{"publish_topic":"/stations/test","sample_rate":5000}} 

If I understand correctly the Lua table will look like this:

{00-06-77-2f-37-94 = "publish_topic":"/stations/test","sample_rate":5000} 

Next I would go through the table to save each value into a local variable.

However, if I try printing out the values of the table (using following code), I get 'nil' as return. Is the code for reading table values wrong? Does the table have two values or is it just the one: ["publish_topic":"/stations/test","sample_rate":5000] ?

lua_value = JSON:decode(data)
  
  for _,d in pairs(lua_value) do
    print(lua_value[d])
  end

local topic = lua_value[0]
local timer = lua_value[1]

end

Edit: I am using following JSON library for Lua: http://regex.info/blog/lua/json

Edit2: @Piglet: I implemented your script and modified it by adding a table (conversionTable) in which both elements "publish_topic":"/stations/test" and "sample_rate:5000" would be respectively saved in the variables pubtop and rate. When I however print each of both variables, nil ist returned in both cases. How can I extract the information out of this table to save in variables?

Ultimately I actually only would like to save the values "/stations/test" and "5000" into these variables. Would I need to parse each of the elements above to get these or is there another way?

local pubtop
local rate
local function printTable(t)
  local conversionTable = {}
  for k,v in pairs(t) do
    if type(v) == "table" then
      conversionTable [k] = string.format("%q: {", k)
      printTable(v)
      print("}")
    else
      print(string.format("%q:", k) .. v .. ",")
    end
  end

  pubtop = conversionTable[0]
  rate = conversionTable[1]
  
end

local lua_value
local function handleOnReceive(topic, data, _, _)
  print("handleOnReceive: topic '" .. topic .. "' message '" .. data .. "'")
  -- This sample publishes the received messages to test/topic2
  print(data)
  lua_value = JSON:decode(data)

  printTable(lua_value)

  print(pubtop)
  print(rate)
end
client:register('OnReceive', handleOnReceive)

解决方案

I don't know which json library you're using so I can't tell you wether JSON:decode(data) is the correct way.

Assuming lua_value would like like so:

local lua_value = {
  ["00-06-77-2f-37-94"] = {
    publish_topic = "/stations/test",
    sample_rate = 5000
  }
}

Then your loop

for _,d in pairs(lua_value) do
    print(lua_value[d])
  end

will indeed print nil.

lua_value has a single element at key "00-06-77-2f-37-94" which is a table.

Each loop iteration will give you a key value pair. So d is actually the value and hence the inner table of lua_value

So you're actually doing this:

local innerTable = lua_value["00-06-77-2f-37-94"]
print(lua_value[innerTable])

Of course lua_value[innerTable] is nil.

Edit:

Try something like

function printTable(t)
  for k,v in pairs(t) do
    if type(v) == "table" then
      print(string.format("%q: {", k))
      printTable(v)
      print("}")
    else
      print(string.format("%q:", k) .. v .. ",")
    end
  end
end

printTable(lua_value)

这篇关于解码并解析JSON到Lua的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆