net.HTTPClient():request如何返回表

时间:2016-11-16 20:29:32

标签: lua

我在Fibaro Home Center 2中使用LUA并希望从net.HTTPClient():request内返回一个表格数组,有人能指出我这样做吗?

代码示例:

roomsArray = {}

-- Get the number of rooms and their names from the Heating Panel
local HC2 = net.HTTPClient();  
 HC2:request('http://127.0.0.1:11111/api/panels/heating', {
   success = function(resp)
    if resp.status == 200 then
      data = json.decode(resp.data)
      for k, v in pairs(data) do 
        table.insert(roomsArray,v.name)          
      end   
      for k, v in pairs(roomsArray) do fibaro:debug("Room index :" .. k .. ", room name is:" .. v) end -- This line works
    else
      fibaro:debug(resp.status)
    end
  end

})

for k, v in pairs(roomsArray) do fibaro:debug("Room index :" .. k .. ", room name is:" .. v) end -- this line produced nothing 
fibaro:debug(roomsArray[1]) -- debug output "nil"

1 个答案:

答案 0 :(得分:0)

您可以使用

 static get properties(){
   return{
     cat:{
       type: String,
       value: "a cat"
       observer: "zeroValue",
     }
   };
 }

 zeroValue(newValue){
  if (newValue) { this.set('cat', newValue; } 
  else          { this.set('cat',"a cat"); }
 }