如何为 dashing.io 解析 JSON 数据

How to parse JSON data for dashing.io

我目前正在为 dashing.io 开发板开发 JSON 解析器。我从 HTTP SSL 连接到我的网络服务器获取信息。我不知道如何正确组合我的输出。

这是我的数据示例:

{ expand: "schema, names",
  startAt: 0,
  issues : [
    0 : {
        expand : "information"
        id: "1231425"
        fields : {
            customfield: "something"
            summary: "short summary one issue"
         },
     1 : {
        expand : "information"
        id: "1231426"
        fields : {
            customfield: "something else"
            summary: "short summary of the other issue"
         }
   ]
}

我想创建一个只列出问题摘要的小部件。我设法得到如下回复:

[[{:summary=>"short summary one issue"}], [{:summary=>"short summary other issue"}]]

但我需要一个看起来像这样的回复:

[{:summary=>"short summary one issue"}, {:summary=>"[short summary other issue"}]

我的数组源代码是:

  (0 .. issue_count[:total] - 1).each do |i|
      @issue_summaries[i] = issue_count
         .map { |json_element| jiraParser.to_json_issue(i)}
   end

我的 JiraParser class,它解析响应正文,看起来像:

 class JiraParser
   def initialize(json_data)
     @data = json_data
   end

   def total
     @data['total']
   end

   def summary(i)
     @issues = @data['issues']
     @issue = @issues[i]
     @fields = @issue['fields']
     @fields['summary']
   end

   def to_json_total
     {:total => total}
    end

    def to_json_issue(i)
      {:summary => summary(i)}
    end
  end

我是 Ruby 的新手,所以我不太了解数据类型等。

你离结果只有一步之遥。因为你有:

ss = [[{:summary=>"short summary one issue"}], 
      [{:summary=>"short summary other issue"}]]

只需调用 flatten 即可:

ss.flatten
#⇒ ss = [{:summary=>"short summary one issue"},
#        {:summary=>"short summary other issue"}]

事实上,您可能会去掉多余的 :summary 并得到一组摘要(假设 issues 包含原始 Hash):

issues[:issues].first.map { |_, v| v[:fields][:summary] }
#⇒  [
#  [0] "short summary one issue",
#  [1] "short summary of the other issue"
# ]