我看到兩個問題:
1)你是森定錯了對象:
response = JSON.parse(json)
name_list[people] = {label: response.keys, value: response.keys[]}
send_event('whosHere', { items: response.values })
name_list[people]
是無法處理您的JSON你的假設結果,但你要發送response.values
代替。
response
是您的parsed-JSON對象返回。你必須遍歷response
構建JSON對象的數組發送到您的列表:
# new empty array of persons each time job is run
persons = []
# since your JSON file is single JSON object, using names for keys
person_names = response.keys
# for each name, do this
person_names.each do |name|
# use the 'name' key to get the value (their status)
person_status = response[name]
# create new Hash/JSON object for each person
# use "label" and "value" keys for Dashing List widget
person = Hash.new("label",name,"value",person_status)
# add this new person object to your persons array
persons.push(person)
end
更新工作,其中persons
陣列發送到請速來代替:
require 'rubygems'
require 'json'
require 'pp'
name_list = Hash.new(0)
SCHEDULER.every '10s' do
json = File.read('list.json')
response = JSON.parse(json)
persons = []
person_names = response.keys
person_names.each do |name|
person_status = response[name]
person = Hash.new("label", name, "value", person_status)
persons.push(person)
end
send_event('whosHere', { items: persons })
end
2)你的JSON ISN」 t格式正確。 Dashing的List小部件正在偵聽具有「label」和「value」鍵的對象數組。每個人都是一個單獨的JSON對象,這個人的名字就是你的「標籤」值,他們的狀態就是他們的「價值」值。
下面是從列表插件的列表項的HTML:
<li data-foreach-item="items">
<span class="label" data-bind="item.label"></span>
<span class="value" data-bind="item.value"></span>
</li>
每個列表項將顯示一個JSON對象:
{
"label":"Mike",
"value":"Here"
}
所以在你的列表中顯示Mike和喬恩的狀態,您將需要一個包含兩個JSON對象的數組:
[
{
"label": "Mike",
"value": "Here"
},
{
"label": "Jon",
"value": "Out"
}
]
此數組將爲「值」爲您的數據對象中的「項目」鍵發送到您的列表部件:
{
"items": [
{
"label": "Mike",
"value": "Here"
},
{
"label": "Jon",
"value": "Out"
}
]
}