将解码的JSON值保存在Lua变量中 [英] Save decoded JSON values in Lua Variables

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

问题描述

以下脚本描述了通过MQTT接收的JSON对象的解码.在这种情况下,我们将以以下JSON对象为例:

Following script describes the decoding of a JSON Object, that is received via MQTT. In this case, we shall take following JSON Object as an example:

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

在handleOnReceive函数中接收并解码后,将使用解码后的对象调用本地函数saveTable,如下所示:

After being received and decoded in the handleOnReceive function, the local function saveTable is called up with the decoded object which looks like:

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

saveTable函数的目标是遍历上表并为表中的值分配"/stations/test".和5000分别分配给变量pubtop和rate.但是,当我同时打印两个变量时,在两种情况下都将返回nil.如何提取该表的值并将其保存在提到的变量中?

The goal of the saveTable function is to go through the table above and assign the values "/stations/test" and 5000 respectively to the variables pubtop and rate. When I however print each of both variables, nil is returned in both cases. How can I extract the values of this table and save them in mentioned variables?

如果我只能保存值"publish_topic ="/stations/test",和"sample_rate = 5000"首先,我需要解析它们以获取上面的值并保存它们,还是有另一种方法?

If i can only save the values "publish_topic = "/stations/test"" and "sample_rate = 5000" at first, would I need to parse these to get the values above and save them, or is there another way?

local pubtop
local rate

local function saveTable(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 .. "'")
  print(data)
  lua_value = JSON:decode(data)

  saveTable(lua_value)

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

要询问的先前问题:将JSON解码并解析为Lua

推荐答案

我给您的功能是递归打印表内容.不必修改以获取某些特定值.您所做的修改没有任何意义.您为什么要将该字符串存储在 conversionTable [k] 中?您显然不知道您在这里做什么.没有冒犯,但您应该继续学习一些基础知识.

The function I gave you was to recursively print table contents. It was not ment to be modified to get some specific values. Your modifications do not make any sense. Why would you store that string in conversionTable[k]? You obviously have no idea what you're doing here. No offense but you should learn some basics befor you continue.

我为您提供了该功能,以便您可以打印json解码的结果.

I gave you that function so you can print whatever is the result of your json decode.

如果您知道自己所期望的,那么递归遍历该表就没有意义了.

If you know you get what you expect there is no point in recursively iterating through that table.

就那样做

for k,v in pairs(lua_value) do
  print(k)
  print(v.publish_topic)
  print(v.sample_rate)
end

现在阅读Lua参考手册,并请做一些初学者教程.如果您不知道如何访问表的元素,那么尝试实现诸如此类的事情就会浪费大量的时间和资源.这就像是Lua中最基本,最重要的操作.

Now read the Lua reference manual and do some beginners tutorials please. You're wasting a lot of time and resources if you're trying to implement things like that if you do not know how to access the elements of a table. This is like the most basic and important operation in Lua.

这篇关于将解码的JSON值保存在Lua变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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