我正在嘗試根據查詢從 SQL Server 表中的資料在 Ruby 中創建 JSON。我用過很多 Ruby 和一些 JSON。但從來沒有在一起。
這是我正在嘗試創建的 JSON 示例。
甚至幫助僅使用嵌套陣列和根元素創建 JSON 也會有所幫助。
{
"aaSequences": [
{
"authorIds": [
"ent_fdfdfdfdf_one"
],
"aminoAcids": "aminoAcids_data",
"name": "bbbbb-22",
"schemaId": "ls_jgjgjg",
"registryId": "src_fgfgfgf",
"namingStrategy": "NEW_IDS"
},
{
"authorIds": [
"ent_fdfdfdfdf_two"
],
"aminoAcids": "aminoAcids_data",
"name": "bbbbb-22",
"schemaId": "ls_jgjgjg",
"registryId": "src_fgfgfgf",
"namingStrategy": "NEW_IDS"
}
]
}
uj5u.com熱心網友回復:
從 Ruby 哈希物件生成 JSON
要生成 json,首先在 Ruby 中以哈希(如字典)開始
my_hash = {:foo => 1, :bar => 2, :baz => 3}
確保你也需要 json 包
require 'json'
然后您可以簡單地將哈希物件轉換為 JSON 字串
my_hash.to_json # outputs: "{'foo': 1, 'bar': 2, 'baz': 3'}"
您也可以將陣列嵌套到哈希中
my_hash_2 = {:foo => [1, 2, 3, 4], :bar => ['a', 'b', 'c', 'd']}
我會讓你自己嘗試那個,但是 ruby?? 會為你很好地處理嵌套物件。
從檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/452092.html
上一篇:如何訪問不同的JSON密鑰?
