Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 使用“key==value”条件搜索(筛选)JSON数组_Ruby - Fatal编程技术网

Ruby 使用“key==value”条件搜索(筛选)JSON数组

Ruby 使用“key==value”条件搜索(筛选)JSON数组,ruby,Ruby,我在ruby中搜索数组中的特定对象时遇到问题 我已向你提出请求https://jsonplaceholder.typicode.com/todos 从那里我得到了结果JSON。我正在尝试将其转换为对象数组,然后搜索我知道可以使用参数发出请求的事件,这将解决我的问题,但我没有访问后端的权限 我试图打印数组中的对象,其中包含一个术语中的特定值,并获取布尔值,说明字符串是否存在于数组中。我还试图在堆栈上找到我的问题的答案。这似乎与我的问题最接近,但对我帮助不大 client = HTTPClient.

我在ruby中搜索数组中的特定对象时遇到问题

我已向你提出请求https://jsonplaceholder.typicode.com/todos 从那里我得到了结果JSON。我正在尝试将其转换为对象数组,然后搜索我知道可以使用参数发出请求的事件,这将解决我的问题,但我没有访问后端的权限

我试图打印数组中的对象,其中包含一个术语中的特定值,并获取布尔值,说明字符串是否存在于数组中。我还试图在堆栈上找到我的问题的答案。这似乎与我的问题最接近,但对我帮助不大

client = HTTPClient.new
method = 'GET'
url = URI.parse 'https://jsonplaceholder.typicode.com/todos'
res = client.request method, url
dd = JSON.parse(res.body)
puts dd.select { |word| word.completed == false }
puts dd.include?('temporibus atque distinctio omnis eius impedit tempore molestias pariatur')
实际结果:

从include返回的select和false完全没有结果

预期结果:

选择应放置到已完成相等的端子对象为false


包括什么?如果数组中存在作为参数提供的值,则应返回true

require 'httpclient'
require 'json'

client = HTTPClient.new
method = 'GET'
url = URI.parse 'https://jsonplaceholder.typicode.com/todos'
res = client.request method, url
dd = JSON.parse(res.body)

# Use word['completed'] instead of word.completed here:
puts dd.select { |word| !word['completed'] }

# Use 'any?' array (Enumerable) method to check
# if any member of the array satisfies given condition:
puts dd.any? { |word| word['title'] == 'temporibus atque distinctio omnis eius impedit tempore molestias pariatur' }

是否需要任何文档?可以在此处找到:

在TypeCode链接中,所有标题条目都缺少行尾的逗号{UserId:1,Id:1,Title:selected要么我看不到问题,也许我遗漏了什么,但当我尝试获取响应时,这看起来非常好,而且当我转到给定链接时,chrome向我显示常规JSON,看起来像:[{UserId:1,Id:1,Title:deletus aut autem,completed:false},....]哦,天哪,我的错。我没有思考就翻译了这个页面,它把格式搞乱了。:facepalm:哇,这真是个好主意。谢谢你的帮助。我正试图检查这个答案是否正确,但我没有正确的声誉分数。不客气。嗯,你应该能够接受一个答案,不管你的声誉如何。试试下面说:好了,谢谢