简体   繁体   English

作为响应,仅访问API的一个属性-Ruby on Rails 4

[英]Access only one attribute of API in response - Ruby on Rails 4

I am calling an API that returns an array of JSON objects and I can access return values of the API call 我正在调用一个返回JSON对象数组的API,并且可以访问该API调用的返回值

[{"param1":1,"param2":"blah1"},
{"param1":2,"param2":"blah2"},
{"param1":3,"param2":"blah3"}]

I know that i can access each Param1 through iteration or by static indexing like @client[0].param1 @client[1].param1 @client[2].param1 but the thing is , i don't want param2 and i want just param1 . 我知道我可以通过迭代或通过@client[0].param1 @client[1].param1 @client[2].param1类的静态索引访问每个Param1,但事实是,我不需要param2并且我想要只是param1。 is there any way , to access param1 without iteration or static indexing so that i could get the below result in response 有什么办法可以访问param1而不进行迭代或静态索引编制,以便我可以得到以下结果作为响应

[{"param1":1},
{"param1":2},
{"param1":3}]

Update 更新资料

The thing to notice is that i want to filter the result while making the request (before getting the response when we know the attribute name) 需要注意的是,我想在发出请求时过滤结果(在我们知道属性名称时获得响应之前)

Try to use delete . 尝试使用delete

Deletes and returns a key-value pair from hsh whose key is equal to key. 从hsh删除并返回其键等于key的键-值对。 If the key is not found, returns the default value. 如果找不到该键,则返回默认值。 If the optional code block is given and the key is not found, pass in the key and return the result of block. 如果给出了可选代码块,但未找到密钥,则传入密钥并返回该块的结果。

data = [{"param1":1,"param2":"blah1"},
{"param1":2,"param2":"blah2"},
{"param1":3,"param2":"blah3"}]

data.each {|x| x.delete("param2")}

For more information about delete . 有关delete的更多信息。 I hope this help you. 希望对您有所帮助。

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

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