Question

I am trying to use forvo.com's API.

I ran this in the console:

response = HTTParty.get('http://apifree.forvo.com/key/XXXXXXXXXXXXXXX/format/json/action/standard-pronunciation/word/%E4%BD%A0%E5%A5%BD/language/zh')

This is the response:

{
    "items" => [
        [0] {
                            "id" => 436,
                       "addtime" => "2008-03-16 10:40:22",
                          "hits" => 4322,
                      "username" => "geneboy",
                           "sex" => "m",
                       "country" => "China",
                          "code" => "zh",
                      "langname" => "Mandarin Chinese",
                       "pathmp3" => "http://apifree.forvo.com/audio/1k1i1b2a3i3a3g38231k3d1m222f2n3c1m1i1i1n1b2e2g2f1f2o3n3p3f2d1n32221j26213q2k341b3k2m2m2a1j281l243m1b271o36293m2l2i252q1b2a233k32_2o341g392o343a3j2i3n2e292o2b1o35221m1i2h3b3n1t1t",
                       "pathogg" => "http://apifree.forvo.com/audio/3d3e3839371h2o242e1p31363g382e3o2f3c351g282m323q2l1j291o2a352h3m1k1m3h3n1i2l1k23273f3n1h3l1m371b3i3k2j312e1m2q2n291m1j3h33273738_2d2k3q1h3e2d2j213p2m2d2m1o3h392h3q2a1g2d3p3n1t1t",
                          "rate" => 5,
                     "num_votes" => 5,
            "num_positive_votes" => 5
        }
    ]
}

How do I print the pathmp3 data?

I tried many things including p response.pathmp3 to no avail.

Was it helpful?

Solution

If I define:

response = {
    "items" => [
        {
                            "id" => 436,
                    "addtime" => "2008-03-16 10:40:22",
                        "hits" => 4322,
                    "username" => "geneboy",
                        "sex" => "m",
                    "country" => "China",
                        "code" => "zh",
                    "langname" => "Mandarin Chinese",
                    "pathmp3" => "http://apifree.forvo.com/audio/1k1i1b2a3i3a3g38231k3d1m222f2n3c1m1i1i1n1b2e2g2f1f2o3n3p3f2d1n32221j26213q2k341b3k2m2m2a1j281l243m1b271o36293m2l2i252q1b2a233k32_2o341g392o343a3j2i3n2e292o2b1o35221m1i2h3b3n1t1t",
                    "pathogg" => "http://apifree.forvo.com/audio/3d3e3839371h2o242e1p31363g382e3o2f3c351g282m323q2l1j291o2a352h3m1k1m3h3n1i2l1k23273f3n1h3l1m371b3i3k2j312e1m2q2n291m1j3h33273738_2d2k3q1h3e2d2j213p2m2d2m1o3h392h3q2a1g2d3p3n1t1t",
                        "rate" => 5,
                    "num_votes" => 5,
            "num_positive_votes" => 5
        }
    ]
}

I'd use:

response['items'].first['pathmp3']
=> "http://apifree.forvo.com/audio/1k1i1b2a3i3a3g38231k3d1m222f2n3c1m1i1i1n1b2e2g2f1f2o3n3p3f2d1n32221j26213q2k341b3k2m2m2a1j281l243m1b271o36293m2l2i252q1b2a233k32_2o341g392o343a3j2i3n2e292o2b1o35221m1i2h3b3n1t1t"

OTHER TIPS

Try this: link

I think that it will help you do do what you want. But you'll have to do something like

require 'json'
response = JSON.encode(response)
puts response.items.first.pathmp3
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top