Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby excel的巨大json对象?_Ruby_Ruby On Rails 3_Ruby On Rails 3.1_Ruby On Rails 3.2_Rubygems - Fatal编程技术网

Ruby excel的巨大json对象?

Ruby excel的巨大json对象?,ruby,ruby-on-rails-3,ruby-on-rails-3.1,ruby-on-rails-3.2,rubygems,Ruby,Ruby On Rails 3,Ruby On Rails 3.1,Ruby On Rails 3.2,Rubygems,我有一个PHP应用程序,它有一个超过10000行的表,我正试图通过我的ROR应用程序将其导出到excel表中,但我的请求在服务器上被PHP应用程序超时。所以我想知道有没有优雅的方法来解决这个问题。我提出了两个解决方案。首先是进行批处理(需要阅读,因为我是新手),另一个是我的php应用程序将发送一个大的json对象,我的ruby应用程序将读取该json对象,将数据写入excel工作表并发送回excel工作表。所以我想问,是否有更好的方法来处理这个问题?如何将json转换为excel我在google

我有一个PHP应用程序,它有一个超过10000行的表,我正试图通过我的ROR应用程序将其导出到excel表中,但我的请求在服务器上被PHP应用程序超时。所以我想知道有没有优雅的方法来解决这个问题。我提出了两个解决方案。首先是进行批处理(需要阅读,因为我是新手),另一个是我的php应用程序将发送一个大的json对象,我的ruby应用程序将读取该json对象,将数据写入excel工作表并发送回excel工作表。所以我想问,是否有更好的方法来处理这个问题?如何将json转换为excel我在google上做过,也发现excel转换为json,但不是相反。有什么建议吗?

我有一些时间,所以我在ruby1.9中构建了一个json到excel的csv转换器:

它从一个文件读取,然后写入另一个文件

json2excel.rb

def json2excel(fromFile, toFile)
  pos = 0
  while true
    c = fromFile.read(1);pos += 1
    if c == ' ' or c == "\n" or c == "\r"
      # whitespace
    elsif c == '['
      # first bracket begins!
      attributes = []
      while true
        c = fromFile.read(1);pos += 1
        if c == '{'
          # now an object starts
          object = Hash.new
          while true
            puts "!!!"
            c = fromFile.read(1);pos += 1
            if c == '"'
              # new attribute starts
              name = ""
              while true
                c = fromFile.read(1);pos += 1
                if c == '"'
                  break
                else
                  name += c
                end
              end
              attributes << name unless attributes.include? name
              # scan for :
              while true
                c = fromFile.read(1);pos += 1
                if c == ':'
                  break
                elsif  c == ' ' or c == '\n' or c == '\r' # whitespace is ok
                else raise "4malformed json for excel conversion! char: #{c.inspect} position: #{pos}"
                end
              end
              # scan for staring value
              while true
                c = fromFile.read(1);pos += 1
                if c == '"'
                  # string follows
                  value = ""
                  value << c
                  while true
                    c = fromFile.read(1);pos += 1
                    value << c
                    if c == '"'
                      break
                    end
                  end
                  c = fromFile.read(1);pos += 1
                  break
                elsif  c == ' ' or c == '\n' or c == '\r' # whitespace is ok
                elsif "1234567890".include? c
                  # number follows
                  value = ""
                  value << c
                  while true
                    c = fromFile.read(1);pos += 1
                    if "1234567890".include? c
                      value << c
                    else break
                    end
                  end
                  break
                elsif c == "t"
                  # true follows
                  c = fromFile.read(3);pos += 3
                  if c != "rue"
                    raise "excpected true but found t#{c.inspect} position: #{pos}"
                  end
                  value = "true"
                  c = fromFile.read(1);pos += 1
                  break
                elsif c == "f"
                  # false follows
                  c = fromFile.read(4);pos += 4
                  if c != "alse"
                    raise "excpected false but found f#{c.inspect} position: #{pos}"
                  end
                  value = "false"
                  c = fromFile.read(1);pos += 1
                  break
                else raise "5malformed json for excel conversion! char: #{c.inspect} position: #{pos}"
                end
              end
              # value starts
              object[name] = value
              puts object
            end
            puts "c: #{c.inspect}"
            if c == "," 
              # comma is ok! just take many of them, does not hurt.
            elsif  c == ' ' or c == '\n' or c == '\r'
              # whitespace is ok
            elsif c == "}"
              # object ends!
              break
            else raise "3malformed json for excel conversion! char: #{c.inspect} position: #{pos}"
            end
          end
          attributes.each{|attr|
            value = object[attr]
            raise "expected object #{object} to have attribute #{attr} position: #{pos}" if value.nil?
            toFile.write(value)
            toFile.write(',')
          }
          toFile.write("\"\"\r\n") # this is the csv new line. a new object begins here
        elsif  c == ' ' or c == '\n' or c == '\r'
          # whitespace is ok
        elsif c == ']'
          attributes.each{ |attr|
            toFile.write(attr.inspect)
            toFile.write(",")
          }
          toFile.write("\"\"\r\n") # this is the csv new line. a new object begins here
          # the end of the file
          c = fromFile.read()
          if c != ''
            raise "end of listing was reached. skipping #{c.size} character after position #{pos}: #{c.inspect}"
          end
          break
        elsif c == ','
          # comma is ok! just take many of them, does not hurt.
        else
          raise "2malformed json for excel conversion! char: #{c.inspect} position: #{pos}"
        end
      end
      break
    else
      raise "1malformed json for excel conversion! char: #{c.inspect} position: #{pos}"
    end
  end
end

json2excel(File.open('json.txt'), File.open('excel.csv', 'wb'))
excel.csv

1,3,"asdf",113,"tyuryt",""
1,3,"asdf",113,"tyuryt",""
"id","pro_id","pro_name","cli_id","cli_name",""
您的列名在文件的末尾

如果在第一个对象之后引入新属性,则并非所有列的元素计数都相等

注意:它不会将所有内容加载到内存中,但会尽快将其写入文件

它没有做什么:

  • 负数
  • 带有
    的数字
  • 包含
    的字符串

您的数据的布局是什么,您能发布前5行重要内容吗?如果这足够的话,您可以利用该布局创建一个.csv。数据将是一个表中的记录。这是csv文件8630,“19190”,“qwer”,“144”,“xyz”,“NULL”,“3”,“2”,“3”,“0”,“16”中的一行,‌​"0","0","0","101","16","415~20130404094900~5.00","0","0.00","5.00","0.00","5.00",‌​“0.00”,“0.00”,“0.00”,“0.00”,“0.00”,“0.00”,“0.00”,“0.00”,“0.00”,“5.00”,“5.00”,空,空,,‌​"11~5",,,,"2~5","11~5","38~5",,,,,,,"38~5"“你的json对象是什么?如果我错了,请纠正我-我的json对象是db表中的一组记录。我不是这一领域的真正专家,但我认为如果我看到了对象,说明了什么应该转换成什么,以及你如何将excel转换成json,你的数据看起来像什么,以及你通常想要实现什么,这将对我有所帮助。如果您没有找到谷歌的解决方案,我们可能需要构建自己的解决方案。
1,3,"asdf",113,"tyuryt",""
1,3,"asdf",113,"tyuryt",""
"id","pro_id","pro_name","cli_id","cli_name",""