解析json以显示仪表板小部件

时间:2015-12-03 08:51:11

标签: ruby ubuntu dashing

我正在尝试下面的代码,我使用了一段时间,因为我不知道数组的长度。但是我得到了这个错误

这个api返回 JSON下面,我想只有一个带有标签的地图,nb_uniq_visitors

查看JSON字符串。

[
  {
    "label": "Australia",
    "nb_uniq_visitors": 1,
    "nb_visits": 1,
    "nb_actions": 2,
    "nb_users": 0,
    "max_actions": 2,
    "sum_visit_length": 519,
    "bounce_count": 0,
    "nb_visits_converted": 0,
    "code": "au",
    "logo": "plugins\/UserCountry\/images\/flags\/au.png",
    "segment": "countryCode==au",
    "logoWidth": 16,
    "logoHeight": 11
  },
  {
    "label": "United States",
    "nb_uniq_visitors": 1,
    "nb_visits": 1,
    "nb_actions": 1,
    "nb_users": 0,
    "max_actions": 1,
    "sum_visit_length": 0,
    "bounce_count": 1,
    "nb_visits_converted": 0,
    "code": "us",
    "logo": "plugins\/UserCountry\/images\/flags\/us.png",
    "segment": "countryCode==us",
    "logoWidth": 16,
    "logoHeight": 11
  }
]

调度程序捕获异常:

  

没有将String隐式转换为Integer   /home//mixed_dashboard/jobs/visitsbycountry.rb:25:in []' /home//mixed_dashboard/jobs/visitsbycountry.rb:25:in阻止'   /var/lib/gems/2.1.0/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:230:in   call' /var/lib/gems/2.1.0/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:230:in trigger_block”   /var/lib/gems/2.1.0/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:204:in   block in trigger' /var/lib/gems/2.1.0/gems/rufus-scheduler-2.0.24/lib/rufus/sc/scheduler.rb:430:in 称之为”   /var/lib/gems/2.1.0/gems/rufus-scheduler-2.0.24/lib/rufus/sc/scheduler.rb:430:in   `block in trigger_job'

----------------------------------------------- ----以下代码----------------------

   require 'rubygems'
   require 'net/http'
   require 'json'

   server = ""

   SCHEDULER.every '30s', :first_in => 0 do |job|  


   url = URI.parse("#{server}")
   http = Net::HTTP.new(url.host, url.port)  
   request = Net::HTTP::Get.new(url.request_uri)
   response = http.request(request)
   j = JSON[response.body]

   # Convert to JSON

   countries = []
   $i=0
   $num = 5

    while $i < $num do 

      country_name = j["label"]
      country_visits = j["nb_unique_visits"]
      countryHash = {country_name => country_visits}
      countries.push(countryHash)
      $i+=1
    end 


   send_event('buzzwords', { items: countries }) 
   end

1 个答案:

答案 0 :(得分:0)

试试这个

$http({
            method: 'POST',
            url: 'server/search/searchQuery.php',
            data: {data: 'getData', searchText: id.SearchText , typeOfStudy: qTypeOfStudy , typeOfSpecies: qTypeOfSpecies , typeOfSpeciality: qTypeOfSpeciality },
            headers: {'Content-Type': 'application/json'},
            dataType:"json"
        }).success(function(data) {