http://hndroidapi.appspot.com/news/format/json/page/?appid=testでリモート json からデータを取得しています。私が直面している問題は、この API が UTF-8 エンコーディングを正しく処理せずに JSON を構築しているように見えることです (ここで間違っている場合は訂正してください)。たとえば、現在渡される結果の一部は
{
"title":"IPad - please don€™t ding while you and I are asleep ",
"url":"http://modern-products.tumblr.com/post/25384729998/ipad-please-dont-ding-while-you-and-i-are-asleep",
"score":"10 points",
"user":"roee",
"comments":"18 comments",
"time":"1 hour ago",
"item_id":"4128497",
"description":"10 points by roee 1 hour ago | 18 comments"
}
に注意してdon€™t
ください。そして、それが窒息している唯一のタイプのキャラクターではありません. API を制御できない場合、データをクリーンなものに変換するためにできることはありますか?
編集:
JSONをプルダウンする方法は次のとおりです。
hn_url = "http://hndroidapi.appspot.com/news/format/json/page/?appid=test"
url = URI.parse(hn_url)
# Attempt to get the json
req = Net::HTTP::Get.new(hn_url)
req.add_field('User-Agent', 'Test')
res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) }
response = res.body
if response.nil?
puts "Bad response when fetching HN json"
return
end
# Attempt to parse the json
result = JSON.parse(response)
if result.nil?
puts "Error parsing HN json"
return
end
編集2:
API の GitHub ページを見つけました。これは未解決の問題のようです。私の側からできる回避策があるかどうかはまだわかりません: https://github.com/glebpopov/Hacker-News-Droid-API/issues/4