Ruby - 解析通过 URL 传入的 JSON

2024-04-02

我正在使用 javascript 将 JSON 对象发送到 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,如下所示:

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']

错误是:

无法将字符串转换为整数

当我尝试将它传递给JSON.parse在 ruby​​ 中,它给了我以下错误:

无法将数组转换为字符串

所以我用了JSON.parse(conf_array.to_json),但是当访问数据时,它再次给出与数组相同的错误:

无法将字符串转换为整数

现在应该做什么?

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).

这是解析 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

gives

{
    "configurations" => [
        [0] {
            "drinks" => [
                [0] {
                    "menus" => [
                        [0] {
                            "hot" => [
                                [0] {
                                          "id" => 15,
                                        "unit" => "0.33",
                                       "price" => "1",
                                    "currency" => "Euro",
                                    "position" => 4
                                },

etc..

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Ruby - 解析通过 URL 传入的 JSON 的相关文章

随机推荐