【问题标题】:How to save Json Array into HTML format in Rails?如何在 Rails 中将 Json 数组保存为 HTML 格式?
【发布时间】:2018-06-06 01:33:25
【问题描述】:

你好 Ruby 用户,我有 Json 数组格式

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

我想使用下面的 html 格式将此数组保存为 html 格式。

<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>

这是我当前的代码,如果我必须只保存数组,它可以正常工作,但是我需要它是 html 格式,以便用户可以轻松阅读它

result = JSON.parse(jsonparse)

result["mods"]["listItems"].each do |result|
    @item = Item.new
    @item.item_details = result["description"]

    @item.save
end

在我以前尝试这样解决

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

这个只保存一个数组,没有&lt;ul&gt;head

这里是原始代码

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

想法是将数组以html格式保存到数据库中。

    <ul>
    <li>content 1</li>
    <li>content 2</li>
    <li>content and soon</li>
    </ul>

谢谢

【问题讨论】:

  • 应该Item 存储整个HTML 吗?
  • 我不确定您的问题。 @Anthony 有很多数据,但我的主要关注点是 item_details,因为它是唯一具有数组的数据。这就是为什么我不需要放置与问题无关的所有其他数据。希望能为你澄清事情。

标签: ruby-on-rails arrays json ruby rake-task


【解决方案1】:

我不确定你想做什么。

请检查以下代码并给我反馈。 快乐编码:)

<!-- language: ruby -->
require 'json'

class MyJsonParser
  def initialize
    @items = []
  end

  def parse(json)
    result = JSON.parse(json)
    generate_items(result)

    items
  end

  private

  attr_reader :items

  def generate_items(result)
    result['mods']['listItems'].each {|item_detail| items << Item.new(item_detail)}
  end
end

class Item
  attr_reader :details

  def initialize(detail='')
    @details = ''
    before_initialize
    details << detail
    after_initialize
  end

  private

  def before_initialize
    details << '<li>'
  end

  def after_initialize
    details << '</li>'
  end
end

json_str = '{
  "mods": {
    "listItems":
      [
        "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"
      ]
  }
}'

result = MyJsonParser.new.parse(json_str)
result.each do |i|
  p i.details
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 2014-12-25
    相关资源
    最近更新 更多