1

Hi everybody this is the string ( sorry if long ) in php

$JsonSpecificheTitoli='{"titoli":{"TitoloEbay-1-Virtuemart":" Maglia Shirt Adidas Milan tg S M L Per sport d è numerico maniche corte Home ","TitoloEbay-2-EbayIT":" Maglia Shirt Adidas Milan tg M L Ibrahimovic Per sport d è numerico Home ","TitoloEbay-5-EbayIT":" Maglia Shirt Adidas Milan tg XL Per sport d è numerico maniche corte Home ","TitoloEbay-3-EbayDE":" Trikot Shirt Adidas Milan tg S M XL F�r Sport ist numerisch S/S Saison 09 10 ","TitoloEbay-6-EbayDE":" Trikot Shirt Adidas Milan tg L F�r Sport ist numerisch S/S Saison 09 10 Home ","TitoloEbay-4-EbayUK":" Jersey Shirt Adidas Milan tg L XL XXL For sports is numerically Kurze armel "},"specifiche":{"Virtuemart":{"":"Arrivo in 24/48 h con SDA oppure Possibilita di necessita di 4/5 giorni per la spedizione","Maniche":" maniche corte "},"EbayIT":{"":"Arrivo in 24/48 h con SDA oppure Possibilita di necessita di 4/5 giorni per la spedizione","Maniche":" maniche corte "},"EbayDE":{"":"Ankunft in 24/48 h M�glichkeit der SDA oder erfordert 4 / 5 Tage Versandkosten","Arme":" S/S "},"EbayUK":{"":"Arrival in 24/48 h Possibility of SDA or requires 4 / 5 days for shipping","Sleeves":" Kurze armel "}}}';

if I make a json_decode from this string directly with php no problem:

$pr = json_decode($JsonSpecificheTitoli);  //-->OK

but If I take it from REQUEST ( from an Ajax request ) it gives me null

$Tt = $_REQUEST['Titoli'];
$TtJ = json_decode ($Tt); // --> it says null

$v=json_last_error();  //--> gives me 5 , is Syntax error? 

Strange anyway how the same string in a REquest and copy and printed can turn it bad?

Thanks

5
  • When posting code on SO, please indent it with four spaces so that it is wrapped in a PRE tag and highlighted. It makes for much easier reading! Commented Dec 28, 2011 at 19:11
  • 2
    Are you passing that data via GET or POST? get is a BAD way to pass long data like this, as most browsers (and servers) limit the length of urls and the json is likely to get chopped off in the middle. Try doing var_dump($_REQUEST['Titoli']);, cut and paste the json you've recevied into jsonlint.com and see what comes up Commented Dec 28, 2011 at 19:12
  • Try var_dump($_REQUEST['Titoli']);. What do you get? Commented Dec 28, 2011 at 19:13
  • Indeed, please can you verify that the contents of $_REQUEST['Titoli'] === $JsonSpecificheTitoli - var_dump() them both and compare the output Commented Dec 28, 2011 at 19:14
  • @PhpMyCoder sorry me is newbie, next time I will try Commented Dec 29, 2011 at 9:49

2 Answers 2

2

$v=json_last_error(); gives me 5 , is Syntax error?

You can check that against the error constants like JSON_ERROR_SYNTAX, just echo out the value:

echo JSON_ERROR_SYNTAX; # 4
echo JSON_ERROR_UTF8;   # 5

So in your case it's an UTF8 error. json_decode­Docs takes an UTF-8 encoded string as input. Just ensure it's UTF-8 encoded and you're fine.

The encoding of $_REQUEST (and similar input variables) depends on the encoding of the HTTP request (input) that is send into your application which is dependent on the encoding/charset of the original website normally.

There is no way to reliably detect it, so it's better you know it. However the mb_http_input­Docs function can be helpful if you need to detect it.

Obtain the information which encoding is used in the HTTP request to your PHP script and then re-encode the incomming data as necessary before passing them into json_decode. You can re-encode a string in PHP with the help of the iconv function­Docs or the Multibyte String functions­Docs.

Additionally you can also verify if the input is valid an valid UTF-8 string, and if not, deny to accept the invalid request to your application. To check if a string is UTF-8, you can run a regular expression on a string:

$isUTF8 = preg_match('//u', $string);

(see also How to detect malformed utf-8 string in PHP?)

Sign up to request clarification or add additional context in comments.

2 Comments

Hi thanks for spent all time reply my fool problem. This community is so nice. Well I will try doing what you say. The problem is that I cannot deny the request as these are internal Data of project and the json call is used to send an easier ajax request. Anyway I try with iconv or Multibyte and see what result Encoding is an hard matter to me :D Thanks!
Ok Solved and after detecting it was a bad formed utf-8 I have used this function function safeJSON_chars($data) { $aux = str_split($data); foreach($aux as $a) { $a1 = urlencode($a); $aa = explode("%", $a1); foreach($aa as $v) { if($v!="") { if(hexdec($v)>127) { $data = str_replace($a,"&#".hexdec($v).";",$data); } } } } return $data; }
1

Check your encoding. 5 = JSON_ERROR_UTF8.

Also, how do you know they are "same string"? Or are you just assuming? Did you print out serverside what the server is receiving, or do you just know what you're sending and expect it to be the same thing that is received?

1 Comment

Hi I have made a print by Netbeans, and tried make a decode of same string. As said I thought it was a syntax error, but due is UTF-8 probably the print already escapes the bad Char. Thanks anyway!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.