我需要将JSON响应转换为格式良好的字符串,更像是表格。
实施例,
[{id: 1, name: "Panda", description: ""}, {id: 2, name: "koala", description: ""},...]
转换为,
Id | Name |Description
1 | Panda |
2 | Koala test |
并将文本自动包装在单元格宽度内。 任何帮助,将不胜感激。 谢谢。
答案 0 :(得分:0)
First you need to parse JSON result.
data = JSON.parse(result)
Then use that parsed result to generate the pattern.
data = [
{ id: 1, name: 'Panda', description: '' },
{ id: 2, name: 'koala', description: '' }
]
puts 'Id | Name |Description'
data.each do |entry|
puts "#{entry[:id]} | #{entry[:name]} |#{entry[:description]}"
end
Hope this helps!