【发布时间】:2014-05-08 04:31:07
【问题描述】:
我的 JSON 提要在这里:
这是该 HTML 页面的 JSON 表示,您可以在页面的副标题中看到相同的 En Dash 字符。
En Dash 在第二个键(描述)中:
description: "In a North Dakota town that was once dying, oil and money are flowing – and bringing big-city problems",
在“流”字之后。
该页面具有以下 HTTP 标头:
Content-Type: application/json;charset=UTF-8
可以通过curl -v或curl -I请求查看
像这样使用 HTTParty 在 Ruby 中下载它:
> r = HTTParty.get('http://america.aljazeera.com/bin/ajam/api/story.json?path=/content/ajam/watch/shows/america-tonight/articles/2014/4/28/the-dark-side-oftheoilboomhumantraffickingintheheartland')
> r['description']
=> "In a North Dakota town that was once dying, oil and money are flowing –\u0080\u0093 and bringing big-city problems"
破坏它,如上所示。经过大量研究,我意识到这是十六进制 utf-8 unicode 值的表示,如下所示:
http://www.fileformat.info/info/unicode/char/2013/index.htm
具体来说,这个:
UTF-8 (hex) 0xE2 0x80 0x93 (e28093)
这些数据稍后会被输入到 iPhone 应用和 Android 应用中。在 Android 应用程序上,它看起来像附加的。在 iPhone 上它看起来不错 - 我认为因为只有第一个字符被渲染,这是一个常规的 Ascii 破折号,接下来的两个字符被跳过。
最后,使用 AJAX 在 JavaScript 中下载它似乎可以正确处理它:
> r = json['description'].match(/flowing (.*) and/)[1]
> "–"
> r
> "–"
> r.length
> 3
> r.toString(16)
> "–"
那么……这是怎么回事?我能做些什么来修复它?是服务器有问题还是我的代码有问题?
【问题讨论】:
-
它没有被“破坏”,这正是 JSON 的工作方式。使用标准 JSON 库来操作它。
-
我明白了。下载后,我做了一些转换,然后在这里公开:d3e8cn1r5u4e7p.cloudfront.net/api/simple?page=6 Android 应用程序从那里下载它。然后 android 应用程序显示它 - 如屏幕截图所示 - 不太好。那么这是否意味着该错误存在于 Android 应用中?
标签: ruby unicode utf-8 character-encoding httparty