images: [
{
path: "http://static.mydomain.de/pics/z.jpg",
format: "image/jpeg",
},
...
...
您的字符串不是 json,因此您无法将其解析为 json。这真的是返回的吗?
如果我尝试通过以下方式提取:
test = open("test.testurl.de/test?p=12").read
puts URI.extract(test)
然后我得到:
["http:", "http:", "http:"]
我得到不同的东西:
require 'uri'
str =<<END_OF_JUNK
images: [
{
path: "http://static.mydomain.de/pics/z.jpg",
format: "image/jpeg",
},
{
path: "http://static.mydomain.de/pics/y.jpg",
format: "image/jpeg",
},
{
path: "http://static.mydomain.de/pics/x.jpg",
format: "image/jpeg",
}
]
END_OF_JUNK
p URI.extract(str)
--output:--
["path:", "http://static.mydomain.de/pics/z.jpg", "format:", "path:", "http://static.mydomain.de/pics/y.jpg", "format:", "path:", "http://static.mydomain.de/pics/x.jpg", "format:"]
有了这个输出,我可以做到:
results = results.select do |url|
url.start_with? "http"
end
p results
--output:--
["http://static.mydomain.de/pics/z.jpg", "http://static.mydomain.de/pics/y.jpg", "http://static.mydomain.de/pics/x.jpg"]
但是,如果您发布的内容是转换为 json 的 ruby 哈希的一部分:
require 'json'
require 'uri'
hash = {
images: [
{
path: "http://static.mydomain.de/pics/z.jpg",
format: "image/jpeg",
},
{
path: "http://static.mydomain.de/pics/y.jpg",
format: "image/jpeg",
},
{
path: "http://static.mydomain.de/pics/x.jpg",
format: "image/jpeg",
}
]
}
str = JSON.dump(hash)
p str
--output:--
"{\"images\":[{\"path\":\"http://static.mydomain.de/pics/z.jpg\",\"format\":\"image/jpeg\"},{\"path\":\"http://static.mydomain.de/pics/y.jpg\",\"format\":\"image/jpeg\"},{\"path\":\"http://static.mydomain.de/pics/x.jpg\",\"format\":\"image/jpeg\"}]}"
然后你可以这样做:
results = URI.extract(str)
p results
--output:--
["http://static.mydomain.de/pics/z.jpg", "http://static.mydomain.de/pics/y.jpg", "http://static.mydomain.de/pics/x.jpg"]