3

My JSON feed is here:

http://america.aljazeera.com/bin/ajam/api/story.json?path=/content/ajam/watch/shows/america-tonight/articles/2014/4/28/the-dark-side-oftheoilboomhumantraffickingintheheartland

It is a JSON representation of this HTML page, you can see the same En Dash character in the subtitle of the page.

http://america.aljazeera.com/watch/shows/america-tonight/articles/2014/4/28/the-dark-side-oftheoilboomhumantraffickingintheheartland.html

The En Dash is in the 2nd key (description):

description: "In a North Dakota town that was once dying, oil and money are flowing – and bringing big-city problems",

after the word "flowing".

The page has the following HTTP header:

Content-Type: application/json;charset=UTF-8

which can be seen by requesting it via curl -v or curl -I

Downloading it in Ruby using HTTParty like so:

> 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"

mangles it, as seen above. After much research I realized is a representation of the hex utf-8 unicode value as seen here:

http://www.fileformat.info/info/unicode/char/2013/index.htm

specifically, this:

UTF-8 (hex) 0xE2 0x80 0x93 (e28093)

This data is later fed into an iPhone app and an Android app. On the the Android app it looks like the attached Android.png. On an iPhone it looks fine - I think because only the first character is rendered and that is a regular Ascii dash, and the next two characters are skipped.

Finally, downloading it in JavaScript using AJAX does seem to handle it correctly:

> r = json['description'].match(/flowing (.*) and/)[1]
> "–"
> r
> "–"
> r.length
> 3
> r.toString(16)
> "–"

So...what is going on? What can I do to fix it? Is the fault with the server or with my code?

2
  • It's not "mangled", it's precisely how JSON works. Use a standard JSON library to manipulate it.
    – tripleee
    May 8, 2014 at 4:44
  • I see. After I download it, I do some transformations and then expose it here: d3e8cn1r5u4e7p.cloudfront.net/api/simple?page=6 from where the Android app downloads it. Then the android app displays it - as seen in the screenshot - not well. So does that mean the bug is in the Android app?
    – Gal
    May 8, 2014 at 4:59

1 Answer 1

3

The JSON feed you're using failed to interpret \u2013 correctly. Instead of generating the desired UTF-8 encoded byte sequence:

E2 80 93

it generated:

E2 80 93 C2 80 C2 93

The reason why the iPhone app works fine may be that it ignores the control character C2 80 and C2 93. However, Android app just render it as some special figure.

You'll need to manually clean those wrong sequence, if you don't have control of the JSON feed.

3
  • 1
    +1 confirmed feed is broken. You can try transcoding to ISO-8859-1 and then reinterpreting those bytes as UTF-8 to fix it, but it would probably be better to report to the webmaster and try to get them to sort it out.
    – bobince
    May 8, 2014 at 10:32
  • Thank you, this is helpful. How did you determine the actual bytes being sent out? (or more importantly, how can I?)
    – Gal
    May 8, 2014 at 20:53
  • 1
    @Gal get the response via wget or browser, save it to file, and use some hex editor to check the byte sequence, such as UltraEditor or vim + xxd.
    – Arie Xiao
    May 9, 2014 at 0:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.