带有多个字符串反斜杠和点的lua的json

问题描述 投票:-1回答:1

你好,我正在尝试将洗衣机中的Json与lua结合使用。用于可视化Domoitcz中的三星。

我从https://api.smartthings.com/v1/devices/abcd-1234-abcd获得的Json的一部分是:

"main": {
"washerJobState": {
            "value": "wash"
},
"mnhw": {
            "value": "1.0"
},
"data": {
          "value": "{
                \"payload\":{
                 \"x.com.samsung.da.state\":\"Run\",\"x.com.samsung.da.delayEndTime\":\"00:00:00\",\"x.com.samsung.da.remainingTime\":\"01:34:00\",\"if\":[\"oic.if.baseline\",\"oic.if.a\"],\"x.com.samsung.da.progressPercentage\":\"2\",\"x.com.samsung.da.supportedProgress\":[\"None\",\"Wash\",\"Rinse\",\"Spin\",\"Finish\"],\"x.com.samsung.da.progress\":\"Wash\",\"rt\":[\"x.com.samsung.da.operation\"]}}"
        },
"washerRinseCycles": {
            "value": "3"
        },
        "switch": {
            "value": "on"
        },

如果我在脚本中使用

local switch = item.json.main.switch.value

我打开或关闭了洗碗池,可以用它来显示洗衣机的状态。

我正在尝试找出如何在脚本中获取“数据”值,还有更多带有反斜线的项目:

local remainingTime = rt.data.value.payload['x.com.samsung.da.remainingTime'] 

local remainingTime = rt.data.value['\payload']['\x.com.samsung.da.remainingTime']

我用'或//,“”尝试了更多的选项,但是总是得到nill值。

有人可以向我解释如何获得:

\“ x.com.samsung.da.remainingTime \”:\“ 01:34:00 \”\“ x.com.samsung.da.progressPercentage \”:\“ 2 \”,

所有的“,\,x。,ar使我困惑

下面是我的脚本,用于测试我只离开了Json日志(基于Dzvents Lua的地方),但出现错误:

dzVents / generated_scripts / Samsung_v3.lua:53:尝试索引一个nil值(全局'json'),我不知道如何使用/调整我的代码来解码字符串。

local json = require"json"  -- the JSON library
local outer = json.decode(your_JSON_string)
local rt = outer.main
local inner = json.decode(rt.data.value)
local remainingTime = inner.payload['x.com.samsung.da.remainingTime']

local API = 'API'
local Device = 'Device'


local LOGGING = true

--Define dz Switches
local WM_STATUS =  'WM Status'  --Domoitcz virtual switch ON/Off state  Washer


return 
{
    on = 
    {
        timer = 
        {
            'every 1 minutes', -- just an example to trigger the request
        },

        httpResponses = 
        {
            'trigger', -- must match with the callback passed to the openURL command
        },
    },

    logging = 
    { 
        level = domoticz.LOG_DEBUG ,
    }, 

    execute = function(dz, item)

        local wm_status = dz.devices(WM_STATUS)

        if item.isTimer then
            dz.openURL({
                url = 'https://api.smartthings.com/v1/devices/'.. Device .. '/states',
                headers = { ['Authorization'] = 'Bearer '.. API },
                method = 'GET',
                callback = 'trigger', -- see httpResponses above.

            })
        end

        if (item.isHTTPResponse) then
            if item.ok then
                if (item.isJSON) then


                   rt = item.json.main
            --      outer = json.decode'{"payload":{"x.com.samsung.da.state":"Run","x.com.samsung.da.delayEndTime":"00:00:00","x.com.samsung.da.remainingTime":"00:40:00","if":["oic.if.baseline","oic.if.a"],"x.com.samsung.da.progressPercentage":"81","x.com.samsung.da.supportedProgress":["None","Weightsensing","Wash","Rinse","Spin","Finish"],"x.com.samsung.da.progress":"Rinse","rt":["x.com.samsung.da.operation"]}}
                inner = json.decode(rt.data.value)
            --        local remainingTime = inner.payload['x.com.samsung.da.remainingTime']


                        dz.utils.dumpTable(rt) -- this will show how the table is structured
            --          dz.utils.dumpTable(inner)

                        local washerSpinLevel = rt.washerSpinLevel.value
            --       local remainingTime = inner.payload['x.com.samsung.da.remainingTime']




                        dz.log('Debuggg washerSpinLevel:' ..  washerSpinLevel, dz.LOG_DEBUG)
                     dz.log('Debuggg remainingTime:' ..  remainingTime, dz.LOG_DEBUG)

            --          dz.log('Resterende tijd:' .. remainingTime, dz.LOG_INFO)
            --          dz.log(dz.utils.fromJSON(item.data))


                  --  end
                elseif LOGGING == true then
                    dz.log('There was a problem handling the request', dz.LOG_ERROR)
                    dz.log(item, dz.LOG_ERROR)
                end
            end
        end
end
}


json lua backslash
1个回答
0
投票

这是一个怪异的构造:普通JSON中的序列化JSON。这意味着您必须调用两次反序列化:

local json = require"json"  -- the JSON library
local outer = json.decode(your_JSON_string)
local rt = outer.main
local inner = json.decode(rt.data.value)
local remainingTime = inner.payload['x.com.samsung.da.remainingTime']
© www.soinside.com 2019 - 2024. All rights reserved.