JSON::ParseError(822:位于';';的意外标记)
我在Ruby中使用JSON,每次运行该程序时,都会出现“意外令牌”错误。我不确定发生了什么,我尝试从其他有相同问题的用户那里读取信息,但我似乎不知道发生了什么 这是我的.rb页面代码:JSON::ParseError(822:位于';';的意外标记),json,ruby,Json,Ruby,我在Ruby中使用JSON,每次运行该程序时,都会出现“意外令牌”错误。我不确定发生了什么,我尝试从其他有相同问题的用户那里读取信息,但我似乎不知道发生了什么 这是我的.rb页面代码: def show URI.parse('http://robotrevolution.net/interface/int_order_options.php') http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = false http.v
def show
URI.parse('http://robotrevolution.net/interface/int_order_options.php')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = false
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
request = Net::HTTP::Post.new(uri.request_uri, initheader = {'Content-Type' => 'application/json'})
request.basic_auth 'username', 'password'
response = http.request(request)
if response.code == "200"
result = response.body.to_json.first
@oc_order_options = JSON.parse(result, :quirks_mode => true)
else
"ERROR"
end
return @oc_order_options
end
这是我的展示页面的代码:
<h1> Display </h1>
<%= @oc_order_options['name'][0]['value'] %>
显示
我真的很感激任何能帮助我了解情况的回复,谢谢。错误在这里:
result = response.body.to_json.first
尝试将put response.body.inspect放在这一行的前面,找出返回的数据,并相应地修改上面的行,以便下一行
JSON.parse(result, :quirks_mode => true)
将成功。mudasobwa是对的,错误出现在result=response.body.to_json上。首先,信息的格式已经是json格式的-这就像是将其双重转换为json,而它不喜欢
所以在JSON.parse(result,:quick\u mode=>true)
我添加了。首先添加到这一行,然后去掉另一行
简单地说,它看起来是这样的:
def show
URI.parse('http://robotrevolution.net/interface/int_order_options.php')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = false
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
request = Net::HTTP::Post.new(uri.request_uri, initheader = {'Content-Type' => 'application/json'})
request.basic_auth 'username', 'password'
response = http.request(request)
if response.code == "200"
result = response.body.to_json.first
@oc_order_options = JSON.parse(result, :quirks_mode => true)
else
"ERROR"
end
return @oc_order_options
结束
现在看起来是这样的:
def show
URI.parse('http://robotrevolution.net/interface/int_order_options.php')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = false
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
request = Net::HTTP::Post.new(uri.request_uri, initheader = {'Content-Type' => 'application/json'})
request.basic_auth 'username', 'password'
response = http.request(request)
if response.code == "200"
result = response.body
@oc_order_options = JSON.parse(result, :quirks_mode => true).first
else
"ERROR"
end
return @oc_order_options
end
谢谢大家! 谢谢你的回复。当我这么做的时候,它向我显示了数据库网站上的所有数据,然后它显示了一条消息“401 Unauthorized”。紧接着它还显示了错误的意外标记。现在你知道发生了什么,应该可以自己解决问题了。哈哈,我真的不知道发生了什么。我可以问你一件事吗?401 Unauthorized是否意味着即使我能够看到所有信息,它也没有授权我使用给定的用户名和密码?是的,Unauthorized意味着Unauthorized。谢谢你的帮助。