Ruby - 解析来自 URL 的 JSON [英] Ruby - parsing JSON coming via URL
问题描述
我正在使用 javascript 向 ruby 发送一个 JSON 对象.但我无法在那里解析它.我尝试了以下内容,但没有运气.此外,我已经搜索了一段时间,但找不到任何有用的东西.
I'm sending a JSON object to ruby with javascript. But I cannot parse it in there. I tried following stuff but no luck. Also I've been searching around for while now, but I couldn't find anything helpful.
请注意,我是全新的 ruby.
Note that I'm very new ruby.
我的试验:
def initialize(game_conf_json)
parsed_conf = JSON.parse(conf_json)
@param1 = parsed_conf['name'][0]
@param2 = parsed_conf['surname'][0]
=begin
I also tried this:
@param1 = parsed_conf['name']
@param2 = parsed_conf['surname']
But no matter what other things I try, I'm getting the following error:
"21:in `[]': can't convert String into Integer (TypeError)"
OR
"can't convert Array into String (TypeError), "
=end
File.open("some_direc/conf.json", "w") do |f|
f.write(game_conf_json)
end
end
我像这样在 javascript 中创建 json:
I create the json in javascript like this:
var json_of_form_vars = {};
json_of_form_vars.name = name_val;
json_of_form_vars.surname = surname_val;
并以这种方式发送:
$.ajax({
url: "../fundament/dispatch.rb/",
type: "post",
dataType: "json",
data: "conf="+json_of_form_vars,
.....
我该如何解决这个问题?是否有针对此问题的适当教程?
How can I solve this problem? Is there any proper tutorial for this of problem?
UPDATE1(经过建议):我使用 JSON.stringify 然后将对象传递给 ruby.然后我终于能够用 ruby 打印对象.如下所示:
UPDATE1 (After suggestions): I used JSON.stringify and then passed the object to ruby. And then I finally able to print the object in ruby. It's listed below:
{"name": "METIN", "surname": "EMENULLAHI"}
方法 .class 声称它是一个数组.但我无法使用经典方式访问数据,例如:
The method .class claims that it is an array. But I cannot access data with classical ways, like:
array['name']
错误是:
无法将字符串转换为整数
can't convert String into Integer
当我尝试将它传递给 ruby 中的 JSON.parse
时,它给了我以下错误:
And when I try to pass it to the JSON.parse
in ruby, it gives me the following error:
无法将数组转换为字符串
can't convert Array into String
所以我使用了 JSON.parse(conf_array.to_json)
,但是在访问数据时它再次给出了与数组相同的错误:
So I used JSON.parse(conf_array.to_json)
, but again when accessing the data it gives the same error like arrays:
无法将字符串转换为整数
can't convert String into Integer
现在应该做什么?
更新2这是我的 cgi 处理程序,它将 URL 参数传递到适当的位置:
UPDATE2 Here is my cgi handler which passes the URL parameters to appropriate places:
cgi_req = CGI::new
puts cgi_req.header
params_from_request = cgi_req.params
logger.error "#{params_from_request}"
action_todo = params_from_request['action'][0].chomp
base = Base.new
if action_todo.eql?('create')
conf_json = params_from_request['conf']
# This line prints the json like this: {"name": "SOME_NAME", "surname": "SOME_SURNAME"}
logger.error "#{conf_json}"
base.create_ident(conf_json)
end
在基类中:
def create_ident(conf_json)
require 'src/IdentityCreation'
iden_create = IdentityCreation.new(conf_json)
end
IdentityCreation
的构造函数在上面列出.
IdentityCreation
's constructor is listed above.
更新 3:
好的,我现在至少从数组中得到了一些东西.但是当我访问一个密钥时,它会显示密钥本身:
Ok, I now get at least something out of the array. But when I access a key, it displays the key itself:
parsed_conf = JSON.parse(conf_json.to_json)
@param1 = parsed_conf[0]['name']
@param2 = parsed_conf[0]['surname']
# At this point when I print the @param1, it gives me "name"(the key), not "SOME_NAME"(the value).
推荐答案
我终于解决了.使用在这篇文章下提出的所有建议,以及我在哈希、数组、json 等方面的 irb 经验.
I solved it finally. Using all the suggestions made under this post and also my irb experiences with hashes, arrays, json and etc.
因此,我没有将对象 (conf_json
) 转换为 json(使用 to_json
),而是将其作为字符串传递给 JSON.parse
如下图:
So instead of converting my object (conf_json
) to json (with to_json
), I passed it to JSON.parse
as a string like below:
parsed_conf = JSON.parse(%{#{conf_json}})
这对我来说似乎有点奇怪,因为当尝试将它传递给如下所示的函数时,我收到了无法将数组转换为字符串
的错误.
It seems kind of weird to me, because when try to pass it to function like below, I got the error of can't convert Array into String
.
parsed_conf = JSON.parse(conf_json)
但它现在就像一个魅力.
But it is working like a charm right now.
这篇关于Ruby - 解析来自 URL 的 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!