你好我是ruby rails的新手,我正在尝试硬编码在我的浏览器中显示来自我使用url的json hash中的短语“java”的“searching_count”,这是以下
{
"results": [
{
"_class": "search_log",
"id": 88,
"phrase": "java",
"searched_count": 3758269,
"url": "/courses/search/?q=java"
},
{
"_class": "search_log",
"id": 296,
"phrase": "javascript",
"searched_count": 2385833,
"url": "/courses/search/?q=javascript"
},
{
"_class": "search_log",
"id": 642,
"phrase": "java programming",
"searched_count": 371310,
"url": "/courses/search/?q=java+programming"
},
{
"_class": "search_log",
"id": 6192,
"phrase": "java for complete beginners",
"searched_count": 193568,
"url": "/courses/search/?q=java+for+complete+beginners"
},
{
"_class": "course",
"id": 478878,
"title": "Java Maven: Introduccion paso a paso para no expertos",
"url": "/java-maven/",
"type_label": "curso"
},
{
"_class": "course",
"id": 1325394,
"title": "Java a Profundidad - Temas Avanzados y Desarrollo Web.",
"url": "/java-a-profundidad/",
"type_label": "curso"
},
{
"_class": "course",
"id": 1187500,
"title": "Java EE 7 & Frameworks - JSF2, Spring 4, Struts 2 y EJB3",
"url": "/java-ee-7-frameworks-jsf2-spring-4-struts-2-y-ejb3/",
"type_label": "curso"
},
{
"_class": "course",
"id": 490376,
"title": "Java para Administradores de Sistemas",
"url": "/java-sysadmin/",
"type_label": "curso"
},
{
"_class": "user",
"id": 28097854,
"title": "Javascript Lab",
"name": "Javascript",
"display_name": "Javascript Lab",
"url": "/user/javascript-lab/",
"type_label": "instructor"
},
{
"_class": "user",
"id": 17677946,
"title": "Lara Javalyn",
"name": "Lara",
"display_name": "Lara Javalyn",
"url": "/user/lara-javalyn/",
"type_label": "instructor"
},
{
"_class": "user",
"id": 7708594,
"title": "Ashay Javadekar",
"name": "Ashay",
"display_name": "Ashay Javadekar",
"url": "/user/ashayjavadekar/",
"type_label": "instructor"
},
{
"_class": "user",
"id": 23997368,
"title": "Alex Javad",
"name": "Alex",
"display_name": "Alex Javad",
"url": "/user/alex-javad-2/",
"type_label": "instructor"
}
]
}
我有两种尝试实现此目的的方法:
require 'sinatra'
require 'net/http'
require 'json'
require 'openssl'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
def get_search_count(term)
url = 'https://www.udemy.com/api-2.0/search-suggestions?q=java'
uri = URI(url)
response = Net::HTTP.get(uri)
return JSON.parse(response)
end
get '/' do
@result = get_search_count "java"
@searchedCount = @result["results"][0]["searched_count"]
"Searched count: #{@searched_count}"
end
当我加载我的本地主机时,我得到的是“搜索计数:”这句话,没有任何内容,任何帮助识别我的错误都非常感谢提前感谢!
答案 0 :(得分:3)
字符串中有拼写错误。您正在阅读@searched_count
而不是@searchedCount