JSON :: ParseError(822:'“处的意外标记)

问题描述 投票:0回答:2

我在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.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'] %>

我非常感谢任何帮助我弄清楚发生了什么的回复,谢谢。

json ruby
2个回答
1
投票

错误在这里:

result = response.body.to_json.first

尝试在此行之前立即放置puts response.body.inspect,找出数据返回的内容并相应地修改上面的行以便下一行

JSON.parse(result, :quirks_mode => true)

将会成功。


0
投票

mudasobwa是对的,错误是在result = response.body.to_json.first信息的格式已经在json - 它就像是将它转换为json,它不喜欢。

所以在JSON.parse(result, :quick_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

谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.