简体   繁体   中英

extract value from nested json

Here is the Jason url

and if you get a xml in your chrome put this inside of a IRB

@results = JSON.parse(open("http://api.arbetsformedlingen.se/af/v0/platsannonser/soklista/lan/", "Accept" => "application/json", "Accept-Language" => "sv").read)

Now I wonder how do I extract nested values from

["platsannons"]["arbetsplats"]["kontaktpersonlista"]["kontaktpersondata"] 

I need to get the name ( ["namn"] ) so that it will display this

Carl Mårtensson 

How do I do this. I have tried a .each do, but that only dispaly it like this

Carl Mårtensson [{"namn"=>"Carl Mårtensson", "telefonnummer"=>""}]

I know that I can use gsub, but that is not good practice :)

EDIT

Here is the documentation and on page 21 to page 23 you will see the Json structure that I am talking about. Documentation

Thanks

First, that URL isn't JSON, but XML. As Arthur says you can use Nokogiri to get the data you want. Let's assume you've read that XML document into a string variable named xml_string . You can then do this:

require 'nokogiri'
doc = Nokogiri::XML(xml_string)
namn = doc.att('arbetsplats kontaktpersonlista kontaktpersondata namn').text

namn will now contain the value Carl Mårtensson

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM