Hallo Ruby-Benutzer i JSON-Array-FormatWie speichere ich Json Array in HTML-Format in Rails?
[
"Can also work with any bluetooth-enabled smartphones and\ntablets",
"For calls and music, Hands-free",
"Very stylish design and lightweight",
"Function:Bluetooth,Noise Cancelling,Microphone",
"Compatible:For Any Device With Bluetooth Function",
"Chipset: CSR4.0", "Bluetooth Version:Bluetooth 4.0",
"Transmission Distance:10 Meters"
]
ich formal dieses Array in HTML mit dem HTML-Format, das unten speichern möchten.
<ul>
<li>Can also work with any bluetooth-enabled smartphones and\ntablets</li>
<li>For calls and music, Hands-free</li>
<li>Very stylish design and lightweight</li>
<li>Function:Bluetooth,Noise Cancelling,Microphone</li>
<li>Compatible:For Any Device With Bluetooth Function</li>
<li>Chipset: CSR4.0</li>
<li>Bluetooth Version:Bluetooth 4.0</li>
<li>Transmission Distance:10 Meters</li>
</ul>
Dies ist meine aktuellen Code, der richtig funktioniert, wenn ich es nur um das Array zu speichern haben aber ich brauche diese HTML-Format zu sein, so Benutzer es leicht
result = JSON.parse(jsonparse)
result["mods"]["listItems"].each do |result|
@item = Item.new
@item.item_details = result["description"]
@item.save
end
Unter meinem Vorherige lesen der Versuch, wie dies
result = JSON.parse(jsonparse)
result["mods"]["listItems"].each do |result|
@item = Item.new
item_list = result["description"]
item_list.each do |list|
@item.item_details = "<li>"+list+"</li>"
end
@item.save
end
Dies zu lösen nur eine der Array speichern und keine <ul>
Kopf
herer der ursprüngliche Code
namespace :scraper do
desc "Scrape Website"
task somesite: :environment do
require 'open-uri'
require 'nokogiri'
require 'json'
url = "url here!"
page = Nokogiri::HTML(open(url))
script = page.search('head script')[2]
jsonparse = script.content[/\{\"[a-zA-Z0-9\"\:\-\,\ \=\(\)\.\_\D\/\[\]\}]+/i]
result = JSON.parse(jsonparse)
result["mods"]["listItems"].each do |result|
@item = Item.new
item_details = result["description"].each {|list| "<li>#{list}</li>" }
puts item_details
@item.item_old_price = result["originalPrice"]
@item.item_final_price = result["price"]
@item.save
end
end
end
Die Idee ist es, das Array in der Datenbank mit dem HTML-Format zu speichern.
<ul>
<li>content 1</li>
<li>content 2</li>
<li>content and soon</li>
</ul>
Dank
sollte ein 'Item' das gesamte HTML speichern? – Anthony
Ich bin mir nicht sicher über Ihre Fragen. @Anthony Es gibt viele Daten, aber mein Hauptfokus ist die 'item_details', da es die einzigen Daten, die Arrays hat. deshalb muss ich nicht alle anderen Daten eingeben, die für die Frage nicht relevant sind. hoffe, dass die Dinge für dich klären. –