RubyonRails:试图从API获取JSON数据并保存到Postgres数据库-rake中止!TypeError:没



我正在使用RubyonRails,并尝试从公共API获取JSON数据并保存到Postgres数据库。

当我运行rake db:seed时,我得到以下错误:

rake aborted!
TypeError: no implicit conversion of String into Integer
/db/seeds.rb:21:in `[]'
/db/seeds.rb:21:in `properties'
/db/seeds.rb:48:in `<top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

作为参考,第21行为:

json["data"].map do |property|

第48行是调用该方法的地方:

properties

这里是JSON数组中的第一个项目,让您了解我试图从中映射的内容。

[
{
"PublicationDate": "26/10/2018",
"PropertyNumber": 2195606,
"County": "LAOIS",
"LocalAuthority": "LAOIS COUNTY COUNCIL",
"Valuation": 70600.0,
"Category": "RETAIL (SHOPS)",
"Uses": "SUPERMARKET 2 [500-2500 SQ. M.], -",
"Address1": "36-42A/1 POUND STREET",
"Address2": "RATHDOWNEY",
"Address3": "CO. LAOIS",
"Address4": "",
"Address5": "",
"CarPark": 0,
"Xitm": 628016.65,
"Yitm": 678231.8,
"ValuationReport": [
{
"Level": "0      ",
"FloorUse": "SUPERMARKET",
"Area": 964.62,
"NavPerM2": 60.0000,
"Nav": 57877.200000
},
{
"Level": "0",
"FloorUse": "OFF LICENCE",
"Area": 1.00,
"NavPerM2": 8681.5800,
"Nav": 8681.580000
},
{
"Level": "0",
"FloorUse": "FIT-OUT ALLOWANCE",
"Area": 1.00,
"NavPerM2": 4051.4000,
"Nav": 4051.400000
}
]
},

我的数据库模式遵循JSON结构,但并不完全如此(我不确定这是否会导致问题(:

create_table "properties", force: :cascade do |t|
t.bigint "user_id"
t.string "publication_date"
t.string "property_number"
t.string "county"
t.string "local_authority"
t.string "valuation"
t.string "category"
t.string "uses"
t.string "address_1"
t.string "address_2"
t.string "address_3"
t.string "address_4"
t.string "address_5"
t.string "car_park"
t.string "xitm"
t.string "yitm"
t.string "valuation_report"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["user_id"], name: "index_properties_on_user_id"
end 

这是我的seeds.rb文件:

require 'rest-client'
# Define Method
def properties
response = RestClient.get('https://api.valoff.ie/api/Property/GetProperties?Fields=*&LocalAuthority=LAOIS%20COUNTY%20COUNCIL&CategorySelected=RETAIL%20(SHOPS)&Format=csv&Download=false')
json = JSON.parse response

if !json.nil?
json["data"].map do |property|
Property.create(
publication_date: "#{PublicationDate}",
property_number: "#{PropertyNumber}",
county: "#{County}",
local_authority: "#{LocalAuthority}",
valuation: "#{Valuation}",
category: "#{Category}",
uses: "#{Uses}",
address_1: "#{Address1}",
address_2: "#{Address2}",
address_3: "#{Address3}",
address_4: "#{Address4}",
address_5: "#{Address5}",
car_park: "#{CarPark}",
xitm: "#{Xitm}",
yitm: "#{Yitm}",
valuation_report: "#{ValuationReport}"
)
end
else
puts "Error seeding properties."
end
end
# Call Method
properties

当我运行rake db:seed时,我得到以下错误:

rake aborted!
TypeError: no implicit conversion of String into Integer
/db/seeds.rb:21:in `[]'
/db/seeds.rb:21:in `properties'
/db/seeds.rb:48:in `<top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

作为参考,第21行为:

json["data"].map do |property|

第48行是调用该方法的地方:

properties

任何帮助都将不胜感激,谢谢

为了总结各种注释中的建议,json是解析JSON后的Array。数组索引是整数,因此json["data"]会产生TypeError。其次,返回的JSON记录没有data字段。您可以简单地对结果数组进行迭代,并创建这样的记录:

def properties
response = RestClient.get('https://api.valoff.ie/api/Property/GetProperties?Fields=*&LocalAuthority=LAOIS%20COUNTY%20COUNCIL&CategorySelected=RETAIL%20(SHOPS)&Format=csv&Download=false')
json = JSON.parse(response)
json.each do |property|
puts "Creating property #{property['PropertyNumber']}"
Property.create!(
publication_date: property['PublicationDate'],
property_number: property['PropertyNumber'],
county: property['County'],
local_authority: property['LocalAuthority'],
valuation_report: property['ValuationReport']
# ... remaining fields omitted for brevity
)
end
end

您不需要在原始文件中进行.nil?检查,因为RestClient会为200-2007以外的任何HTTP响应代码引发错误。同样,如果响应不是有效的JSON,JSON.parse将引发错误。

结果:

$ rails db:seed
Creating property 1555903
Creating property 1556133
Creating property 1556998
Creating property 1556516
Creating property 1557007
...

最新更新