如何打印 JSON 响应中的特定值



我正在尝试使用forvo.com的API。

我在控制台中运行了这个:

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

这是响应:

{
    "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
        }
    ]
}

如何打印pathmp3数据?

我尝试了很多事情,包括p response.pathmp3无济于事。

如果我定义:

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
        }
    ]
}

我会使用:

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

试试这个:链接

我认为它会帮助你做你想做的事。但是你必须做一些类似的事情

require 'json'
response = JSON.encode(response)
puts response.items.first.pathmp3

相关内容

  • 没有找到相关文章

最新更新