json - Map object and nested object to model using Ruby on Rails -


i have object

{"result":[{     "links":[{             "urlto":"http://www.example.com/",             "visited":1364927598,             "firstseen":1352031217,             "prevvisited":1362627231,             "anchor":"example.com",             "type":"text",             "flag":[],             "textpre":"",             "textpost":""         }],     "index":0,     "rating":0.001416,     "urlfrom":"http://www.exampletwo.com",     "ipfrom":"112.213.89.105",     "title":"example title",     "linksinternal":91,     "linksexternal":51,     "size":5735 }]} 

and have model of keys.

urlto, visited, firstseen, prevvisited, anchor, type, textpre, textpost, index, rating, urlfrom, ipfrom, title, linksinternal, linksexternal, size

i understand how save database without bit below...

"links":[{             "urlto":"http://example.com/",             "visited":1364927598,             "firstseen":1352031217,             "prevvisited":1362627231,             "anchor":"example.com",             "type":"text",             "flag":[],             "textpre":"",             "textpost":""         }], 

not sure how save nested object well.

i had search on google , , couldn't find anything, correct way this? should move nested object 1 above? have no need nested...

thanks in advance

it looks want save links, loop on result/links in json provided, , create new hash based on links.

i've pretended below json in file called input.json -- you'd parse text or use existing json object

require 'json' json = json.parse file.read("input.json")  links = json["result"].map |result|   result["links"].map {|link| link } end.flatten  hash = {"links" => links} puts hash 

this creates object:

{"links"=>[{"urlto"=>"http://www.example.com/", "visited"=>1364927598, "firstseen"=>1352031217, "prevvisited"=>1362627231, "anchor"=>"example.com", "type"=>"text", "flag"=>[], "textpre"=>"", "textpost"=>""}]} 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -