简体   繁体   English

如何通过 Ruby on Rails 中的 API GET 请求访问(并保存到我的数据库)JSON 数组中的嵌套对象/属性?

[英]How can I access (and save to my database) nested objects/properties within a JSON array via an API GET request in Ruby on Rails?

How can I access (and save to my postgresQL database) nested objects/properties within a JSON array via an API GET request in Ruby on Rails?如何通过 Ruby on Rails 中的 API GET 请求访问(并保存到我的 postgresQL 数据库)JSON 数组中的嵌套对象/属性?

I have accessed all of the properties such as PropertyNumber etc. and this is saved to my database and I have it displaying on my front end.我已经访问了所有属性,例如 PropertyNumber 等,并将其保存到我的数据库中,并将其显示在我的前端。 I also want to use the information to perform calculations etc. I can do this with all properties of the JSON object except for the ValuationReport property which contains nested objects.我还想使用这些信息来执行计算等。我可以使用 JSON 对象的所有属性执行此操作,但包含嵌套对象的 ValuationReport 属性除外。

The ValuationReport property appears to be an array object containing one or many ValuationReport objects (each containing 5 properties - Level, FloorUse, Area, NavPerM2 and Nav). ValuationReport 属性似乎是一个数组对象,其中包含一个或多个 ValuationReport 对象(每个对象包含 5 个属性 - Level、FloorUse、Area、NavPerM2 和 Nav)。 I would like to access this information and save each individually to my database in the same way as I have done with the other properties.我想访问这些信息并将每个信息单独保存到我的数据库中,就像我对其他属性所做的一样。 At the moment it seems to be saved to my database as an object or a string and is displaying on the front end like this:目前它似乎作为对象或字符串保存到我的数据库中,并在前端显示如下:

[{"Level"=>"1", "FloorUse"=>"OFFICE(S)", "Area"=>23.89, "NavPerM2"=>70.0, "Nav"=>1672.3}, {"Level"=>"0", "FloorUse"=>"OFFICE(S)", "Area"=>74.57, "NavPerM2"=>100.0, "Nav"=>7457.0}] [{"Level"=>"1", "FloorUse"=>"OFFICE(S)", "Area"=>23.89, "NavPerM2"=>70.0, "Nav"=>1672.3}, {"Level"= >"0", "FloorUse"=>"OFFICE(S)", "Area"=>74.57, "NavPerM2"=>100.0, "Nav"=>7457.0}]

Here's my seeds.rb file:这是我的 seed.rb 文件:

require 'rest-client'
require 'json'

# Define Method - API Request
def properties

    response = RestClient.get('https://api.valoff.ie/api/Property/GetProperties?Fields=*&LocalAuthority=CARLOW%20COUNTY%20COUNCIL&CategorySelected=OFFICE&Format=json&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: property['Valuation'],
            category: property['Category'],
            uses: property['Uses'],
            address_1: property['Address1'],
            address_2: property['Address2'],
            address_3: property['Address3'],
            address_4: property['Address4'],
            address_5: property['Address5'],
            car_park: property['CarPark'],
            xitm: property['Xitm'],
            yitm: property['Yitm'],
            valuation_report: property['ValuationReport']
        )
    end
end

# Call Method
properties

Here's my schema.rb file:这是我的 schema.rb 文件:

  create_table "properties", force: :cascade do |t|
    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
  end

I expect that I will need to add the 5 individual properties to my schema (Level, FloorUse, Area, NavPerM2 and Nav) in order to save them individually.我希望我需要将 5 个单独的属性添加到我的架构(Level、FloorUse、Area、NavPerM2 和 Nav)中,以便单独保存它们。 Or maybe they can be accessed from the ValuationReport attribute and be parsed in a controller for calculations or in the html.erb file for clean display.或者,它们可以从 ValuationReport 属性访问,并在控制器中解析以进行计算或在 html.erb 文件中进行解析以进行清晰显示。 I'm not sure about this.我不确定这一点。

Thank you for reading this and for your help.感谢您阅读本文并提供帮助。

This was resolved by creating a Property and then using its ID in Units:这是通过创建一个属性然后在单位中使用其 ID 来解决的:

require 'rest-client'
require 'json'

# Define Method - API Request
def properties

    response = RestClient.get('https://api.valoff.ie/api/Property/GetProperties?Fields=*&LocalAuthority=CAVAN%20COUNTY%20COUNCIL&CategorySelected=OFFICE&Format=csv&Download=false')
    json = JSON.parse(response)

    json.each do |property|
        puts "Creating property #{property['PropertyNumber']}"

        property_model = Property.create!(

            publication_date: property['PublicationDate'],
            property_number: property['PropertyNumber'],
            county: property['County'],
            local_authority: property['LocalAuthority'],
            valuation: property['Valuation'],
            category: property['Category'],
            uses: property['Uses'],
            address_1: property['Address1'],
            address_2: property['Address2'],
            address_3: property['Address3'],
            address_4: property['Address4'],
            address_5: property['Address5'],
            car_park: property['CarPark'],
            xitm: property['Xitm'],
            yitm: property['Yitm']
        )


        property['ValuationReport'].each do |unit|
          puts "Creating unit."
          property_model.units.create!(
            level: unit['Level'],
            floor_use: unit['FloorUse'],
            area: unit['Area'],
            nav_per_m2: unit['NavPerM2'],
            nav: unit['Nav']
          )
        end
    end
end

# Call Method
properties

Reference: Ruby on Rails: How can I use JSONPath to access (and save to database) nested objects/properties within a JSON array?参考: Ruby on Rails:如何使用 JSONPath 访问(并保存到数据库)JSON 数组中的嵌套对象/属性?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby on Rails:如何使用 JSONPath 访问(并保存到数据库)JSON 数组中的嵌套对象/属性? - Ruby on Rails: How can I use JSONPath to access (and save to database) nested objects/properties within a JSON array? 如何访问数据库内部和数组中的单个属性-使用Ruby on Rails - How do I access a single attribute inside and array within my database - working with Ruby on Rails 如何在后端的 Rails 上使用 Ruby 在发布请求中访问嵌套的 json 参数 - How to access nested json params in a post request with Ruby on Rails on the backend 如何在Ruby on Rails中访问深层嵌套的对象? - How to access a deep nested objects in Ruby on Rails? 如何在Ruby on Rails中收到嵌套属性的JSON:API帖子? - How can I receive a JSON:API post of nested attributes in Ruby on Rails? AssociationTypeMisMatch:如何使用接受JSON的RESTful API(Rails)创建对象和关联的(嵌套的)对象? - AssociationTypeMisMatch: How can I create an object and associated (nested) objects with RESTful API (Rails) accepting JSON? 删除嵌套在 rails 上的对象数组 Ruby 中的属性 - delete attributes nested within an array of objects Ruby on rails Ruby on Rails表单:如何拆分字符串并将其另存为数组? - Ruby on Rails Form: How can I split a String and save it as an Array? 如何在 Rails 上的 Ruby 中将每个元素的嵌套属性获取到数组中? - How can I get a nested attribute of each element to an array in Ruby on Rails? 将JSON从API端点保存到Ruby on Rails应用程序中的数据库 - Save JSON from API endpoint to database in Ruby on Rails app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM