3
我用下面的方法來從英語一個簡單的詞翻譯成俄語致電:紅寶石 - 微軟翻譯意外的標記錯誤
translate("hello")
這是我的方法:
def translate(text)
begin
uri = "http://api.microsofttranslator.com/V2/Ajax.svc/GetTranslations?appId=#{@appid}&text=#{text.strip}&from=en&to=ru&maxTranslations=1"
page = HTTParty.get(uri).body
show_info = JSON.parse(page) # this line throws the error
rescue
puts $!
end
end
JSON輸出:
{"From":"en","Translations":[{"Count":0,"MatchDegree":100,"MatchedOriginalText":"","Rating":5,"TranslatedText":"Привет"}]}
錯誤:
unexpected token at '{"From":"en","Translations":[{"Count":0,"MatchDegree":100,"MatchedOriginalText":"","Rating":5,"TranslatedText":"Привет"}]}'
不確定這是什麼意思unexpected token
。這是我收到的唯一錯誤。不幸的是,我不能修改JSON輸出,因爲它是由API本身返回的。
UPDATE:
貌似API將返回一些非法字符(壞微軟):
'´╗┐{"From":"en","Translations":[{"Count":0,"MatchDegree":0,"Matched OriginalText":"","Rating":5,"TranslatedText":"Hello"}]}'
完整的錯誤:
C:/Ruby193/lib/ruby/1.9.1/json/common.rb:148:in `parse': 743: unexpected token at '´╗┐{"From":"en","Translations":[{"Count":0,"MatchDegree":0,"Matched
OriginalText":"","Rating":5,"TranslatedText":"Hello"}]}' (JSON::ParserError)
from C:/Ruby193/lib/ruby/1.9.1/json/common.rb:148:in `parse'
from trans.rb:13:in `translate'
from trans.rb:17:in `<main>'
這不是一個Rails的問題,它看起來並不像一個API或翻譯問題提出。您沒有傳遞關於引發錯誤的信息。它是JSON行嗎?請將該示例簡化爲最小的失敗問題並重新標記您的問題。 – 2012-01-09 14:17:00
@MarkThomas積分。原始問題已更新。 – fuzz 2012-01-10 00:38:09
@Fulvio這是完整的錯誤信息嗎? – Dogbert 2012-01-10 09:51:56