https://www.googleapis.com/freebase/v1/search?query=madonna#
The JSON result is breaking PHPs json_decode. To be exact, the following string is breaking decoding: "Sticky \x26amp; Sweet Tour".
Browsers however seem to be able to understand it: http://jsfiddle.net/nggX2/ & http://jsfiddle.net/QUVFt/
http://jsonlint.com/ claims it's invalid JSON.
On PHP's side I've tried: http://codepad.viper-7.com/suUbQD and http://codepad.viper-7.com/QjqCH7
Any thoughts on what's going on?
What's going on is that this is invalid JSON. The response from that url is incorrect--JSON doesn't allow the \xXX two-digit hexadecimal binary escape sequences, only \uXXXX unicode code point escape sequences. Here it should just be &, though--no escape sequence needed.
No idea why google/freebase is outputting invalid JSON.
Your JSON should look like the following:
"Sticky \\x26amp; SweetTour"
The slash needs to be escaped, because it is the escape char.
Related
In PHP, is it at all possible to output the contents of a string to show any escaped characters that may be contained within the string? I get that the whole point of escaping characters is so that they aren't treated in the usual way. But I would still like to be able to view the raw contents of a string so I can see for myself exactly how characters like \n and \r, etc. are represented. Does PHP have a method for doing this?
Use json_encode() to encode the string as JSON. The JSON encoding of strings (which is, in fact, JavaScript) is the same as the one used by PHP. Both JavaScript and PHP were inspired from C and they copied the notation of string literals from it.
if you use single quotation marks it should do what you need
eg echo 'this\n'; will output this\n where as echo "this\n"; will output this and a new line
My PHP application outputs JSON where special characters are encoded, f.ex. the string "Brøndum" is represented as "Br\u00f8ndum".
Can you tell me which encoding this is, as well as how I get back from "Br\u00f8ndum" to "Brøndum".
I have tried utf8_encode/decode but they don't work as expected.
Thanks!
That's standard JSON unicode escaping.
You get back to the actual character by using a JSON parser. json_decode in the case of PHP.
You can tell PHP not to escape Unicode characters in the first place with the JSON_UNESCAPED_UNICODE flag.
json_encode("Brøndum", JSON_UNESCAPED_UNICODE)
mb_detect_encoding is your function. You just pass it the string and it detects the codification. You can also send it an array with the possibilities (as a regular string like "hello" could potentially be encoded in different codifications.
echo mb_detect_encoding("Br\u00f8ndum");
I pass php array like
[sit_latitude] => 20° 23.298' N
inside json_encode(); and output is like,
{"sit_latitude":null}
it's display null, how to resolve this?
my code snippet
...
$stmt->bindParam("id", $_GET[id]);
$stmt->execute();
$employee = $stmt->fetchObject();
$dbh = null;
echo '{"item":'. json_encode($employee) .'}';
what i tried and succeed was bit strange but worked , so let me share it with you.
$latitude = json_encode(utf8_encode($emplyoee['sit_latitude']));
$longitude = json_encode(utf8_encode($employee['sit_longitude']));
$emplyoee['sit_latitude'] = $latitude;
$emplyoee['sit_longitude'] = $longitude;
and now pass like
echo '{"item":'. json_encode($employee) .'}';
this will solve your problem
json_encode assumes that strings in its input are valid UTF-8 (keep in mind that PHP strings are sequences of bytes, not sequences of Unicode code points). If your text editor is not set to use that encoding (and you haven't manually generated the UTF-8 using hex escape codes such as \xc2\xb0 in a double-quoted string), the string will be encoded as null (because it is not valid UTF-8).
Other common charsets such as Windows-1252 are also a superset of ASCII, so this is only a problem if there are any non-ASCII characters (U+00B0 DEGREE SIGN included).
Even if the string is valid UTF-8, by default, json_encode will output \u00b0 instead of the actual degree sign. This is not a problem for programmatic decoding using PHP json_decode or JavaScript JSON.parse, which understand JSON hex escape sequences.
(In PHP 5.4, there is a JSON_UNESCAPED_UNICODE flag to prevent this escaping. However, many web servers still have PHP 5.3 or older, and the flag unescapes even the problematic characters U+2028 LINE SEPARATOR and U+2029 PARAGRAPH SEPARATOR. It is possible to simulate the flag in two lines of code, although as I explained above, you don't have to anyway.)
JSON and escaping characters - this post may help you.
Just pass degree as \u00f8C and then decode it with js.
You could try htmlspecialchars() function http://php.net/manual/en/function.htmlspecialchars.php
i have this JSON string that i want to decode it with json_decode(); function
{"phase":2,"id":"pagelet_profile_picture","css":["VCxcl","Ix2pq"],"js":["fZYUE","VfnZ3"],"content":{"pagelet_profile_picture":"\u003cdiv class=\"profile-picture\">\u003cspan class=\"profile-picture-overlay\">\u003c\/span>\u003cimg class=\"photo img\" src\=\"http:\/\/profile.ak.fbcdn.net\/hprofile-ak-snc4\/222_111_2222_n.jpg\" alt=\"bla bla\" id=\"profile_pic\" \/>\u003c\/div>"}}
there is the json_last_error(); but it not helping me. (got JSON_ERROR_STATE_MISMATCH and JSON_ERROR_SYNTAX sometimes)
i want to know what wrong with this JSON string and how i can fix it automatically in PHP so i can decode it.
some code will be very helpful
thanks.
Using a json lint, it seems the problem is the src\=
the \ escapes the = sign, which makes no sense.
If you replace src\= with src= it passes the validator.
The fix:
Fix the code that generates the json string in the first place.
or
use str_replace to change 'src\=' to 'src='
The problem with a wrong encoding is that it's just a wrong encoding. Things then break.
If the problem is related to invalid escape sequences as Ben pointed out in his answer, you can try to fix the input string for these sequences, probably with a smarter algorithm that is looking for any not-needed escape sequence replacing it with it's non-escaped value by removing the escape character \.
You can do so by creating a list of characters that need actual to be escaped, then parse the whole string for the escape character, if found, check if the next character requires escaping or not and then act upon.
However that's only one strategy and as the input is not properly encoded, it's not easy to just fix things because they are already broken.
I am using JSON_ENCODE in PHP to output data.
When it gets to this word: Æther it outputs \u00c6ther.
Anyone know of a way to make json output that character or am I going to have to change the text to not have that character in it?
That's the unicode version of the character. JavaScript should handle it properly. You'll notice the slash before it which means that it's an escape sequence. The u indicates it's a unicode code point and the hex digits represent the actual character.
See here for some more info.
That is working as specified. The RFC ( http://www.ietf.org/rfc/rfc4627.txt ) indicates that any character may be escaped, and your average printable character can be written in the \uXXXX format.
Any JSON parser that cannot understand a character escaped in that way is not compliant with the standard. Work on resolving that problem rather than trying to coax PHP into misbehaving as well.
(It is legal to put UTF-8 characters into JSON strings without escaping them as well, with a few exceptions, but the safe approach of escaping anything questionable is wise.)