我正在用javascript向ruby发送一个JSON对象。但我不能在那里解析它。我尝试了以下的东西,但没有运气。我现在一直在寻找,但我找不到任何有用的东西。
请注意,我是一个非常新的红宝石。
我的试验:
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,如下所示:
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,
.....
我该如何解决这个问题?这个问题是否有适当的教程?
UPDATE1(建议后): 我使用JSON.stringify然后将对象传递给ruby。然后我终于能够在ruby中打印对象了。它列在下面:
{"name": "METIN", "surname": "EMENULLAHI"}
方法.class声称它是一个数组。但我不能用经典方法访问数据,例如:
array['name']
错误是:
无法将String转换为整数
当我尝试将其传递给ruby中的JSON.parse
时,它会给我以下错误:
无法将Array转换为String
所以我使用了JSON.parse(conf_array.to_json)
,但在访问数据时它再次给出了与数组相同的错误:
无法将String转换为整数
现在该怎么办?
UPDATE2 这是我的cgi处理程序,它将URL参数传递到适当的位置:
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
的构造函数。
UPDATE3:
好的,我现在至少从阵列中得到了一些东西。但是当我访问密钥时,它会显示密钥本身:
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).
答案 0 :(得分:1)
这是解析JSON字符串的示例。如果仍有问题,请发布生成的JSON,以便我们可以尝试。
require 'json'
require 'ap'
string = %{
{"configurations" : [
{ "drinks" : [
{"menus" : [
{ "hot" : [
{"id":15,"unit":"0.33", "price":"1", "currency":"Euro", "position": 4},
{"id":15,"unit":"0.33", "price":"1", "currency":"Euro", "position": 6}
] },
{ "cold" : [
{"id":15,"unit":"0.33", "price":"1", "currency":"Euro", "position": 4},
{"id":15,"unit":"0.33", "price":"1", "currency":"Euro", "position": 6}
] },
{ "terminals" : { "id" : 4, "id": 6, "id": 7 } },
{ "keys" : { "debit" : "on", "credit": "off" } }
] }
] } ] }
}
hash = JSON.parse(string)
ap hash
给出
{
"configurations" => [
[0] {
"drinks" => [
[0] {
"menus" => [
[0] {
"hot" => [
[0] {
"id" => 15,
"unit" => "0.33",
"price" => "1",
"currency" => "Euro",
"position" => 4
},
等。
答案 1 :(得分:1)
目前你还没有发布json。你正试图在表单编码参数中发布json。另外,当你做
时"conf=" + json_of_form_vars
javascript会将json_of_form_vars
转换为字符串,但转换与转储到JSON不同。 Javascript默认字符串转换对于对象来说是无用的,因此您需要使用JSON.stringify来获取实际的json。
由于您将主体组成字符串文字,因此您还需要在此上下文中转义任何不允许(或具有特殊含义)的特殊字符。让jquery做繁重的工作通常更容易,比如
$.ajax({
url: "../fundament/dispatch.rb/",
type: "post",
dataType: "json",
data: {conf: JSON.stringify(json_of_form_vars)}
答案 2 :(得分:0)
我终于解决了。使用在这篇文章下提出的所有建议以及我对哈希,数组,json等的irb经验。
所以我没有将我的对象(conf_json
)转换为json(带to_json
),而是将其作为字符串传递给JSON.parse
,如下所示:
parsed_conf = JSON.parse(%{#{conf_json}})
对我来说这似乎有点奇怪,因为当我尝试将其传递给下面的函数时,我得到了can't convert Array into String
的错误。
parsed_conf = JSON.parse(conf_json)
但它现在就像一个魅力。