Note that there are some explanatory texts on larger screens.

plurals
  1. POUnescaping characters in a string with Ruby
    text
    copied!<p>Given a string in the following format (the Posterous API returns posts in this format):</p> <pre><code>s="\\u003Cp\\u003E" </code></pre> <p>How can I convert it to the actual ascii characters such that <code>s="&lt;p&gt;"</code>?</p> <p>On OSX, I successfully used <code>Iconv.iconv('ascii', 'java', s)</code> but once deployed to Heroku, I receive an <code>Iconv::IllegalSequence</code> exception. I'm guessing that the system Heroku deploys to does't support the <code>java</code> encoder.</p> <hr> <p>I am using <a href="https://github.com/jnunemaker/httparty" rel="nofollow">HTTParty</a> to make a request to the Posterous API. If I use curl to make the same request then I do <em>not</em> get the double slashes. </p> <p>From HTTParty github page: </p> <blockquote> <p>Automatic parsing of JSON and XML into ruby hashes based on response content-type</p> </blockquote> <p>The Posterous API returns JSON (no double slashes) and HTTParty's JSON parsing is inserting the double slash.</p> <hr> <p>Here is a simple example of the way I am using HTTParty to make the request.</p> <pre><code>class Posterous include HTTParty base_uri "http://www.posterous.com/api/2" basic_auth "username", "password" format :json def get_posts response = Posterous.get("/users/me/sites/9876/posts&amp;api_token=1234") # snip, see below... end end </code></pre> <p>With the obvious information (username, password, site_id, api_token) replaced with valid values.</p> <p>At the point of snip, <code>response.body</code> contains a Ruby string that is in JSON format and <code>response.parsed_response</code> contains a Ruby hash object which HTTParty created by parsing the JSON response from the Posterous API.</p> <p>In both cases the unicode sequences such as <code>\u003C</code> have been changed to <code>\\u003C</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload