当我尝试访问json中的数据转换为ruby中的哈希时,如何将字符串转换为整数?

时间:2021-09-04 15:29:54

I have converted the data in json to a hash and tried to access the data. I used the following code:

我已将json中的数据转换为哈希并尝试访问数据。我使用了以下代码:

require 'yajl'
json=File.new('5104.txt', 'r')
parser=Yajl::Parser.new
hash=parser.parse(json)
puts hash['response']['venue']['id']
puts hash['response']['venue']['name']
puts hash['response']['venue']['categories']['parents']

When I run this code, I had this error message says:

当我运行此代码时,我有此错误消息说:

test.rb:8:in `[]': can't convert String into Integer (TypeError)
    from test.rb:8:in `<main>'

I assume it means that the data type of 'parents' is string which cannot be converted to integer. Does anyone know how should I solve this problem? Is there anyway I can convert this string into integer and save it?

我认为这意味着'parents'的数据类型是不能转换为整数的字符串。有谁知道我该如何解决这个问题?无论如何我可以将此字符串转换为整数并保存吗?

Thanks ahead and I really appreciate your help.

在此,非常感谢您的帮助。


Thanks a lot for your answers. Line 8 is the last line for 'parents'. I can use this code to get 'id' and 'name', but cannot get the data for 'parent'. Here is the json;

非常感谢你的回答。第8行是“父母”的最后一行。我可以使用此代码获取'id'和'name',但无法获取'parent'的数据。这是json;

response: {
     venue: {
        id: "xxx"
        name: "xxx"
        contact: {
           phone: "xxx"
           formattedPhone: "xxx"
           twitter: "theram"
        }
        location: {
           address: "xxx"
           lat: xxx
           lng: xxx
           postalCode: "xxx"
           city: "xxx"
           state: "xxx"
           country: "xxx"
        }
        categories: [
           {
              id: "xxx"
              name: "xxx"
              pluralName: "xxx"
              shortName: "xxx"
              icon: "xxx"
              parents: [
                 "xxx"
              ]
              primary: true
           }
        ]
     }
}

I converted the json to a hash. If it is the case that this json is an array which requires integers for its keys, is there anyway that I can convert this string into an integer so that I can still use this code to get the data I want? If not, is there any other ways that I can get data for 'parents'? Regular expression?

我将json转换为哈希。如果这个json是一个需要整数键的数组的情况,那么无论如何我可以将这个字符串转换成一个整数,这样我仍然可以使用这个代码来获取我想要的数据吗?如果没有,有没有其他方法可以获取“父母”的数据?正则表达式?

Thanks ahead :)

谢谢你:)

2 个解决方案

#1


3  

Categories is an array, you should get an item before trying to get the parents:

类别是一个数组,你应该在尝试获取父母之前得到一个项目:

hash['response']['venue']['categories'].first['parents']

There's also a primary parameter, if there could be more than one category and want to get the primary one, use Array#select:

还有一个主要参数,如果可能有多个类别并且想要获得主要参数,请使用Array#select:

hash['response']['venue']['categories'].select {|category| category['primary'] } .first['parents']

#2


0  

Are you absolutely positive that what you're getting out of the JSON is a hash? Because that error message generally means you're trying to access an array with a non-integer key.

你绝对肯定你从JSON中获得的是哈希吗?因为该错误消息通常意味着您尝试使用非整数键访问数组。

I'm guessing that your JSON is actually an array, and ruby is dutifully complaining that you're not treating it like one.

我猜你的JSON实际上是一个数组,并且ruby正在尽职尽责地抱怨你没有把它当成一个。

#1


3  

Categories is an array, you should get an item before trying to get the parents:

类别是一个数组,你应该在尝试获取父母之前得到一个项目:

hash['response']['venue']['categories'].first['parents']

There's also a primary parameter, if there could be more than one category and want to get the primary one, use Array#select:

还有一个主要参数,如果可能有多个类别并且想要获得主要参数,请使用Array#select:

hash['response']['venue']['categories'].select {|category| category['primary'] } .first['parents']

#2


0  

Are you absolutely positive that what you're getting out of the JSON is a hash? Because that error message generally means you're trying to access an array with a non-integer key.

你绝对肯定你从JSON中获得的是哈希吗?因为该错误消息通常意味着您尝试使用非整数键访问数组。

I'm guessing that your JSON is actually an array, and ruby is dutifully complaining that you're not treating it like one.

我猜你的JSON实际上是一个数组,并且ruby正在尽职尽责地抱怨你没有把它当成一个。