Ruby 如何正确格式化此json?

Ruby 如何正确格式化此json?,ruby,json,Ruby,Json,翻译此数据对象的json代码时遇到问题 var dataObject = { "timeline": { "headline":"The Main Timeline Headline Goes here", "type":"default", "text":"<p>Intro body text goes here, some HTML is ok</p>", "a

翻译此数据对象的json代码时遇到问题

  var dataObject = {
      "timeline":
      {
          "headline":"The Main Timeline Headline Goes here",
          "type":"default",
          "text":"<p>Intro body text goes here, some HTML is ok</p>",
          "asset": {
              "media":"http://yourdomain_or_socialmedialink_goes_here.jpg",
              "credit":"Credit Name Goes Here",
              "caption":"Caption text goes here"
          },
          "date": [
              {
                  "startDate":"2011,12,10",
                  "endDate":"2011,12,11",
                  "headline":"Headline Goes Here",
                  "text":"<p>Body text goes here, some HTML is OK</p>",
                  "asset": {
                      "media":"http://twitter.com/ArjunaSoriano/status/164181156147900416",
                      "thumbnail":"optional-32x32px.jpg",
                      "credit":"Credit Name Goes Here",
                      "caption":"Caption text goes here"
                  }
              }
          ],
          "era": [
              {
                  "startDate":"2011,12,10",
                  "endDate":"2011,12,11",
                  "headline":"Headline Goes Here",
                  "text":"<p>Body text goes here, some HTML is OK</p>",
              }
          ]
      }
  }
具体来说,我不确定

t['timeline']['date'] = [{}]

我应该如何正确地写这些行

t['timeline']['date'] = [{}]
那应该行得通。只需添加稍微不同的属性即可。像这样:

t['timeline']['date'][0]['startDate'] = "2011,12,10"
                     ^^^
                     first element in the array

直接构建一个散列:

def timeline
  {
    "timeline" = {
      "headline" = "Lorem",
      "text" = "default",
      "asset" = {}
    },
    "date" = [{
      "startDate" = "2011,12,10",
      "asset" = {
        "media" = ""
      }
    }]
  }
end

节省了大量的键入,并且在心理上将更自然地映射到JSON。

您的哈希语法无效:)
t['timeline']['date'][0]['startDate'] = "2011,12,10"
                     ^^^
                     first element in the array
def timeline
  {
    "timeline" = {
      "headline" = "Lorem",
      "text" = "default",
      "asset" = {}
    },
    "date" = [{
      "startDate" = "2011,12,10",
      "asset" = {
        "media" = ""
      }
    }]
  }
end