0

I try to decode a string with json_decode() function.

$in = $_POST['json'];
$jsonIn = json_decode($in);

for example

$in = '{"company":"q","address":"q","phone":"q","mobile":"q","email":"q"}';

if I try to force the raw string, it works fine, but if I use $in instead, it doesn't

the proof that it does / doesn't is the fact that if I try to access for example

$jsonIn->company

in the first case it returns me null, in the second one it returns me "q".

anybody can explain? thanks in advance Dario

Dario Rusignuolo
  • 2,090
  • 6
  • 38
  • 68
  • How do you get JSON in the post? – zerkms Jul 16 '12 at 23:25
  • 7
    can you show `var_dump($_POST['json'])` – Martin Jul 16 '12 at 23:27
  • $.ajax({type: 'POST', url: 'url.php',data: {json : JSON.stringify(json2Send)}, – Dario Rusignuolo Jul 16 '12 at 23:30
  • 1
    From what I read here it's the magic_quotes thingy: http://blog.thefrontiergroup.com.au/2008/11/json_decode_php/ – Tim Vermaelen Jul 16 '12 at 23:30
  • @TimVermaelen yeah, I was just about to suggest that. Especially likely if you're runnig on PHP 5.2, since it's on by default then. If you've got control of the server I suggest disabling magic quotes, they're evil http://php.net/manual/en/security.magicquotes.php – John Carter Jul 16 '12 at 23:32
  • string '{"company":"q","address":"q","phone":"q","mobile":"q"}' (length=140) – Dario Rusignuolo Jul 16 '12 at 23:33
  • possible duplicate of [json_decode returns NULL (php)](http://stackoverflow.com/questions/689185/json-decode-returns-null-php), specifically http://stackoverflow.com/a/2197308/8331 – John Carter Jul 16 '12 at 23:46
  • I have disabled magic quotes and the other json_decode that contains an array is giving me an error. {"items":["final product"]} if I check $jsonIn->items[0]=='final product' it returns me false. but if I echo $jsonIn->items[0] it gives me "final product". here is the
    string 'final product' (length=15)
    
    – Dario Rusignuolo Jul 16 '12 at 23:51
  • 1
    just added json_decode($in,true) and it worked fine – Dario Rusignuolo Jul 16 '12 at 23:54
  • @DarioRusignuolo, create an answer yourself and accept it. – Shoe Jul 17 '12 at 00:39
  • @Dario Rusignuolo: you still didn't answer - why do you have JSON in POST? Who sends it? – zerkms Jul 17 '12 at 02:20
  • @DarioRusignuolo can you show the data from a print_r wrapped in pre `";print_r(json_decode($_POST['json']));echo ""; ?>` – classicjonesynz Jul 17 '12 at 03:17
  • Jeffrey: done. zerkms: sent from javascript. I simplyfied the json, but it's a little more complx 'cause there are some arrays in it. Killrawr: you want me to post what u want even if the problem was solved? – Dario Rusignuolo Jul 19 '12 at 13:03
  • @Killrawr I modified the code, so I can't restore my php file at that situation – Dario Rusignuolo Jul 19 '12 at 13:08
  • @DarioRusignuolo if the problem has already been solved then, don't worry about it. :) regards – classicjonesynz Jul 20 '12 at 03:59

1 Answers1

0

The problem was resolved after disabling the Magic Quotes.

Robert
  • 8,717
  • 2
  • 27
  • 34
Dario Rusignuolo
  • 2,090
  • 6
  • 38
  • 68