Ruby 读取文件并转换为json数组
只要换线就行了Ruby 读取文件并转换为json数组,ruby,arrays,json,file-io,Ruby,Arrays,Json,File Io,只要换线就行了 { "strings" : [ "334455", "ABC" ] } 例如: array.concat(line.split.map(&:to_s)) 只要换线就行了 { "strings" : [ "334455", "ABC" ] } 例如: array.concat(line.split.map(&:to_s)) 只要换线就行了 { "strings" : [
{
"strings" : [
"334455",
"ABC"
]
}
例如:
array.concat(line.split.map(&:to_s))
只要换线就行了
{
"strings" : [
"334455",
"ABC"
]
}
例如:
array.concat(line.split.map(&:to_s))
只要换线就行了
{
"strings" : [
"334455",
"ABC"
]
}
例如:
array.concat(line.split.map(&:to_s))
只要换线就行了
{
"strings" : [
"334455",
"ABC"
]
}
例如:
array.concat(line.split.map(&:to_s))
或者为什么不做类似的事情
array = []
File.foreach(file.txt) do |line|
array.concat(line.split.map(&:to_s))
end
@test = {
"strings" => array
}.to_json
或者为什么不做类似的事情
array = []
File.foreach(file.txt) do |line|
array.concat(line.split.map(&:to_s))
end
@test = {
"strings" => array
}.to_json
或者为什么不做类似的事情
array = []
File.foreach(file.txt) do |line|
array.concat(line.split.map(&:to_s))
end
@test = {
"strings" => array
}.to_json
或者为什么不做类似的事情
array = []
File.foreach(file.txt) do |line|
array.concat(line.split.map(&:to_s))
end
@test = {
"strings" => array
}.to_json
做同样事情的一种更为ruby的方法是:
@test = {
"strings" => File.open("file.txt",&:read).split("\n").flat_map(&:split)
}.to_json
#=> "{\"strings\":[\"this\",\"is\",\"a\",\"test\",\"file\",\"putting\",\"strings\",\"into\",\"an\",\"array\"]}"
JSON.parse(@test)
#=> {"strings"=>["this", "is", "a", "test", "file", "putting", "strings", "into", "an", "array"]}
做同样事情的一种更为ruby的方法是:
@test = {
"strings" => File.open("file.txt",&:read).split("\n").flat_map(&:split)
}.to_json
#=> "{\"strings\":[\"this\",\"is\",\"a\",\"test\",\"file\",\"putting\",\"strings\",\"into\",\"an\",\"array\"]}"
JSON.parse(@test)
#=> {"strings"=>["this", "is", "a", "test", "file", "putting", "strings", "into", "an", "array"]}
做同样事情的一种更为ruby的方法是:
@test = {
"strings" => File.open("file.txt",&:read).split("\n").flat_map(&:split)
}.to_json
#=> "{\"strings\":[\"this\",\"is\",\"a\",\"test\",\"file\",\"putting\",\"strings\",\"into\",\"an\",\"array\"]}"
JSON.parse(@test)
#=> {"strings"=>["this", "is", "a", "test", "file", "putting", "strings", "into", "an", "array"]}
做同样事情的一种更为ruby的方法是:
@test = {
"strings" => File.open("file.txt",&:read).split("\n").flat_map(&:split)
}.to_json
#=> "{\"strings\":[\"this\",\"is\",\"a\",\"test\",\"file\",\"putting\",\"strings\",\"into\",\"an\",\"array\"]}"
JSON.parse(@test)
#=> {"strings"=>["this", "is", "a", "test", "file", "putting", "strings", "into", "an", "array"]}
在您的
File.open{..}
之后,您是否检查了array
的输出?这是错误的array Ah blindness。。。就是这样<代码>阵列正常,。。。现在检查放置数组
,在块之后。数组
现在良好@test
返回[[“33445”],[“ABC”]
在您的文件打开{..}
后,您是否检查了数组的输出
?这是错误的数组啊盲。。。就是这样<代码>阵列正常,。。。现在检查放置数组
,在块之后。数组
现在良好@test
返回[[“33445”],[“ABC”]
在您的文件打开{..}
后,您是否检查了数组的输出
?这是错误的数组啊盲。。。就是这样<代码>阵列正常,。。。现在检查放置数组
,在块之后。数组
现在良好@test
返回[[“33445”],[“ABC”]
在您的文件打开{..}
后,您是否检查了数组的输出
?这是错误的数组啊盲。。。就是这样<代码>阵列正常,。。。现在检查放置数组
,在块之后。数组
现在良好<代码>@test
return[[“33445”],[“ABC”]]
readlines
与我的功能相同,但外观更干净+1。readlines
与我的功能相同,但外观更干净+1。readlines
与我的功能相同,但外观更干净+1。readlines
与我的功能相同,但外观更干净看起来更干净+1。