问题
This is part of my JSON file I obtain through the twitter API:
"text":"Scientists discover new method for studying molecules: Queen\u2019s researchers have discovered the method for studyi... http://bit.ly/chbweE"
I am using PHP for my project.
After using the json_decode function, \u2019 is converted into ’, which basically is really annoying.
I tried using $raw_json = preg_replace("u2019","'",$raw_json)
, but then the json_decode
function returns NULL.
Is there any other way I can convert the \u2019 into ' ?
Thanks
Edit:
This is how I am obtaining the JSON:
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "http://search.twitter.com/search.json?q=from%3Agizmodo&rpp=10");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$raw_json = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
回答1:
This is probably a problem with the charset you're using in your output. If you're outputing to an HTML page, try adding the following meta tag to your head section:
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
回答2:
You're missing the regex delimiters /.../
in preg_replace
. Try:
$raw_json = preg_replace("/u2019/","'",$raw_json);
Also, to be even safer, I'd only replace those instances that are actually escape characters:
$raw_json = preg_replace("/\\\\u2019/","\\'",$raw_json);
来源:https://stackoverflow.com/questions/4211806/json-from-twitter-api-contains-u2019