2013-05-19 4 views
0

Возникли проблемы перевода моего JSON кода формы этого DataObject ...Как правильно отформатировать этот 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>", 
       } 
      ] 
     } 
    } 

в этот метод ...

def timeline  
    t = {} 
    t['timeline'] = {} 
    t['timeline']['headline'] = "Lorem" 
    t['timeline']['text'] = "default" 
    t['timeline']['asset'] = {} 
    t['timeline']['asset']['media'] = "" 
    t['timeline']['asset']['credit'] = "" 
    t['timeline']['asset']['caption'] = "" 

    t['timeline']['date'] = [{}] 
    t['timeline']['date']['startDate'] = "2011,12,10" 
    t['timeline']['date']['endDate'] = "2011,12,11" 
    t['timeline']['date']['headline'] = "" 
    t['timeline']['date']['text'] = "" 
    t['timeline']['date']['asset'] = {} 
    t['timeline']['date']['asset']['media'] = "" 
    t['timeline']['date']['asset']['thumbnail'] = "" 
    t['timeline']['date']['asset']['credit'] = "" 
    t['timeline']['date']['asset']['caption'] = "" 

    t['timeline']['era'] = [{}] 
    t['timeline']['era']['startDate'] = "2011,12,10" 
    t['timeline']['era']['endDate'] = "2011,12,11" 
    t['timeline']['era']['headline'] = "" 
    t['timeline']['era']['text'] = "" 

    return t 
end 

конкретно я неопределенный о

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

и

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

Как правильно писать эти строки?

ответ

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

, который должен работать просто отлично. Только вы должны добавлять атрибуты немного разные. Как это:

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

Просто построить хэш непосредственно:

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

Сохраняет много печатать, и мысленно карту гораздо более естественно JSON.

+0

Ваш синтаксис hash недействителен :) –

+0

Doh, это так. Тем не менее, точки стоят. :) –

+0

nope .... this дает ... "синтаксическая ошибка, неожиданная '=', ожидающая tASSOC" – thefonso

Смежные вопросы