簡體   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?

如何通過 Ruby on Rails 中的 API GET 請求訪問(並保存到我的 postgresQL 數據庫)JSON 數組中的嵌套對象/屬性?

我已經訪問了所有屬性,例如 PropertyNumber 等,並將其保存到我的數據庫中,並將其顯示在我的前端。 我還想使用這些信息來執行計算等。我可以使用 JSON 對象的所有屬性執行此操作,但包含嵌套對象的 ValuationReport 屬性除外。

ValuationReport 屬性似乎是一個數組對象,其中包含一個或多個 ValuationReport 對象(每個對象包含 5 個屬性 - Level、FloorUse、Area、NavPerM2 和 Nav)。 我想訪問這些信息並將每個信息單獨保存到我的數據庫中,就像我對其他屬性所做的一樣。 目前它似乎作為對象或字符串保存到我的數據庫中,並在前端顯示如下:

[{"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}]

這是我的 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

這是我的 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

我希望我需要將 5 個單獨的屬性添加到我的架構(Level、FloorUse、Area、NavPerM2 和 Nav)中,以便單獨保存它們。 或者,它們可以從 ValuationReport 屬性訪問,並在控制器中解析以進行計算或在 html.erb 文件中進行解析以進行清晰顯示。 我不確定這一點。

感謝您閱讀本文並提供幫助。

這是通過創建一個屬性然后在單位中使用其 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

參考: Ruby on Rails:如何使用 JSONPath 訪問(並保存到數據庫)JSON 數組中的嵌套對象/屬性?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM