简体   繁体   English

如何访问 json / ruby 中的键值

[英]How to access key, value in json / ruby

I have a json file like this, and I try to display the different value in my html:我有一个像这样的 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",
        }
    ]
}

Could someone help me, for example, to access to display the value 'Alexis'有人可以帮助我,例如,访问以显示值“亚历克西斯”

This is not valid JSON due to the trailing commas within your hashes.由于散列中的尾随逗号,这不是有效的 JSON。 If you fix the commas, minify the JSON to make it easier to work with, and save it as a string, then you can begin to work with it in Ruby:如果修复逗号,将 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"}]}'

Now parse it into a real Ruby object:现在将其解析为真正的 Ruby object:

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"
        }
    ]
}

This is a hash that has an array of hashes inside it.这是一个 hash,里面有一个哈希数组。 You should access it using the standard methods for Hash and Array .您应该使用HashArray的标准方法访问它。

Start by getting the value of the only key in the hash, which is an array:首先获取 hash 中唯一键的值,这是一个数组:

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"
    }
]

You indicated you wanted to fetch a value from index 4:您表示要从索引 4 中获取值:

sub_hash = array[4]
=> {
     "office" => "Consultant",
    "authors" => "Alexis"
}

And that you wanted to return the string Alexis :并且您想返回字符串Alexis

string = sub_hash['authors']
=> "Alexis"

Or put it all together in one line:或者将它们放在一行中:

string = hash['testimonials'][4]['authors']
=> "Alexis"

Or one even shorter line:或者更短的一行:

JSON.parse(json)['testimonials'][4]['authors']
=> "Alexis"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM