如何在Rails中将Json数组保存为HTML格式?

时间:2023-01-14 10:31:52

Hello Ruby users i have Json Array format

你好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"
]

I want to save this array into html formal using the html format which is below.

我想使用下面的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>

This is my current code which is working correctly if i have to save it just the array however i need this to be html format so user can easily read it

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

result = JSON.parse(jsonparse)

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

    @item.save
end

Under my Previous Attempt to solve like this

在我之前尝试解决这样的问题

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

This one only save one of the array and no <ul> head

这个只保存一个数组而没有

heres the original 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

The Idea is to save the Array into database with the html format.

想法是使用html格式将Array保存到数据库中。

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

Thanks

1 个解决方案

#1


0  

I'm not sure what you tried to do.

我不确定你想做什么。

Please check below codes and give me a feedback. Happy coding :)

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

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

#1


0  

I'm not sure what you tried to do.

我不确定你想做什么。

Please check below codes and give me a feedback. Happy coding :)

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

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