如何访问 json / ruby 中的键值
How to access key, value in json / ruby
我有一个这样的 json 文件,我尝试在 html 中显示不同的值:
{
"testimonials": [
{
"office": "Test",
"authors": "Benjamin",
},
{
"office": "consultant",
"authors": "Maxime ",
},
{
"office": "DAF",
"authors": "Alexandre",
},
{
"office": "CEO",
"authors": "Raphaël",
},
{
"office": "Consultant",
"authors": "Alexis",
},
{
"office": "CEO,",
"authors": "Sylvain",
}
]
}
有人可以帮助我,例如,访问显示值 'Alexis'
这是无效的 JSON,因为您的散列中有尾随逗号。如果您修复逗号,缩小 JSON 以使其更易于使用,并将其保存为字符串,那么您可以在 Ruby:
中开始使用它
json = '{"testimonials":[{"office":"Test","authors":"Benjamin"},{"office":"consultant","authors":"Maxime "},{"office":"DAF","authors":"Alexandre"},{"office":"CEO","authors":"Raphaël"},{"office":"Consultant","authors":"Alexis"},{"office":"CEO,","authors":"Sylvain"}]}'
现在将其解析为真正的Ruby对象:
hash = JSON.parse(json)
=> {
"testimonials" => [
[0] {
"office" => "Test",
"authors" => "Benjamin"
},
[1] {
"office" => "consultant",
"authors" => "Maxime "
},
[2] {
"office" => "DAF",
"authors" => "Alexandre"
},
[3] {
"office" => "CEO",
"authors" => "Raphaël"
},
[4] {
"office" => "Consultant",
"authors" => "Alexis"
},
[5] {
"office" => "CEO,",
"authors" => "Sylvain"
}
]
}
这是一个散列,其中包含一个散列数组。您应该使用 Hash and Array.
的标准方法访问它
首先获取散列中唯一键的值,它是一个数组:
array = hash['testimonials']
=> [
[0] {
"office" => "Test",
"authors" => "Benjamin"
},
[1] {
"office" => "consultant",
"authors" => "Maxime "
},
[2] {
"office" => "DAF",
"authors" => "Alexandre"
},
[3] {
"office" => "CEO",
"authors" => "Raphaël"
},
[4] {
"office" => "Consultant",
"authors" => "Alexis"
},
[5] {
"office" => "CEO,",
"authors" => "Sylvain"
}
]
您表示要从索引 4 中获取值:
sub_hash = array[4]
=> {
"office" => "Consultant",
"authors" => "Alexis"
}
你想要 return 字符串 Alexis
:
string = sub_hash['authors']
=> "Alexis"
或者将它们全部放在一行中:
string = hash['testimonials'][4]['authors']
=> "Alexis"
或者更短的一行:
JSON.parse(json)['testimonials'][4]['authors']
=> "Alexis"
我有一个这样的 json 文件,我尝试在 html 中显示不同的值:
{
"testimonials": [
{
"office": "Test",
"authors": "Benjamin",
},
{
"office": "consultant",
"authors": "Maxime ",
},
{
"office": "DAF",
"authors": "Alexandre",
},
{
"office": "CEO",
"authors": "Raphaël",
},
{
"office": "Consultant",
"authors": "Alexis",
},
{
"office": "CEO,",
"authors": "Sylvain",
}
]
}
有人可以帮助我,例如,访问显示值 'Alexis'
这是无效的 JSON,因为您的散列中有尾随逗号。如果您修复逗号,缩小 JSON 以使其更易于使用,并将其保存为字符串,那么您可以在 Ruby:
中开始使用它json = '{"testimonials":[{"office":"Test","authors":"Benjamin"},{"office":"consultant","authors":"Maxime "},{"office":"DAF","authors":"Alexandre"},{"office":"CEO","authors":"Raphaël"},{"office":"Consultant","authors":"Alexis"},{"office":"CEO,","authors":"Sylvain"}]}'
现在将其解析为真正的Ruby对象:
hash = JSON.parse(json)
=> {
"testimonials" => [
[0] {
"office" => "Test",
"authors" => "Benjamin"
},
[1] {
"office" => "consultant",
"authors" => "Maxime "
},
[2] {
"office" => "DAF",
"authors" => "Alexandre"
},
[3] {
"office" => "CEO",
"authors" => "Raphaël"
},
[4] {
"office" => "Consultant",
"authors" => "Alexis"
},
[5] {
"office" => "CEO,",
"authors" => "Sylvain"
}
]
}
这是一个散列,其中包含一个散列数组。您应该使用 Hash and Array.
的标准方法访问它首先获取散列中唯一键的值,它是一个数组:
array = hash['testimonials']
=> [
[0] {
"office" => "Test",
"authors" => "Benjamin"
},
[1] {
"office" => "consultant",
"authors" => "Maxime "
},
[2] {
"office" => "DAF",
"authors" => "Alexandre"
},
[3] {
"office" => "CEO",
"authors" => "Raphaël"
},
[4] {
"office" => "Consultant",
"authors" => "Alexis"
},
[5] {
"office" => "CEO,",
"authors" => "Sylvain"
}
]
您表示要从索引 4 中获取值:
sub_hash = array[4]
=> {
"office" => "Consultant",
"authors" => "Alexis"
}
你想要 return 字符串 Alexis
:
string = sub_hash['authors']
=> "Alexis"
或者将它们全部放在一行中:
string = hash['testimonials'][4]['authors']
=> "Alexis"
或者更短的一行:
JSON.parse(json)['testimonials'][4]['authors']
=> "Alexis"