Welcome Guest, Not a member yet? Register   Sign In
JSON PROB
#1

[eluser]kptengco[/eluser]
I've got a request data from other domain (json format). Its not empty or null and whenever i do this.

Code:
$_REQUEST['jsonData'] = '{"name":"Person 1"}'; //from other domain

     $dataDecoded = json_decode($_REQUEST['jsonData']);

     $dataDecoded->name; // this result to empty but if i print the $dataDecoded it has the data.
     print_r($dataDecoded); 'name' => 'Person 1'

The question is why this
Code:
$dataDecoded->name;
is empty or not showing "Person 1" value.



This is the request data from the other domain:

Code:
{"SC_MC":"00000000","SC_MID":"00000000","SC_AMOUNT":5000,"SC_REF":"0000","SC_PAYMODE":"Regular Installment","SC_PAYTERM":"12","SC_TRANSIP":"undefined","SC_PAYREF":"000000000000","SC_PAYDATE":"2012-05-21","SC_REFERRER":"https:\/\/abc.abc.com\/abc\/abc\/abc.php","SC_AGENT":"Mozilla\/5.0 (Windows NT 6.1; rv:12.0) Gecko\/20100101 Firefox\/12.0","SC_RRN":null,"SC_CUR_DATA":"PHP|1","SC_ORIG_AMOUNT":5000,"SC_STATUS":"approved"}

is the data format wrong?
#2

[eluser]CroNiX[/eluser]
Are you sure it's empty? You aren't echoing it out or doing anything with it. Basically you're just making a statement that has no action.

Code:
$dataDecoded->name;
#3

[eluser]kptengco[/eluser]
im inserting it to a table and result is empty but when i insert this
Code:
$_REQUEST['jsonData']

it has its values
Code:
{"SC_MC":"00000000","SC_MID":"00000000","SC_AMOUNT":5000,"SC_REF":"0000"
,"SC_PAYMODE":"Regular Installment","SC_PAYTERM":"12","SC_TRANSIP":"undefined","SC_PAYREF":"000000000000"
,"SC_PAYDATE":"2012-05-21","SC_REFERRER":"https:\/\/abc.abc.com\/abc\/abc\/abc.php","SC_AGENT":"Mozilla
\/5.0 (Windows NT 6.1; rv:12.0) Gecko\/20100101 Firefox\/12.0","SC_RRN":null,"SC_CUR_DATA":"PHP|1"
,"SC_ORIG_AMOUNT":5000
,"SC_STATUS":"approved"}

just like this
Code:
$dataDecoded = json_decode($_REQUEST['jsonData']);

"INSERT INTO tbl (field1) VALUES('".addslashes($dataDecoded->name)." | ".$_REQUEST['jsonData']."')";

//when i view the table the data was there but the $dataDecoded->name is empty
I tried to select that data to my table then use "json_decode" it works. The question is why its not decoding at first its only decoding after it has recorded in my table and select then decode it.

Code:
"SELECT dataCode FROM tbl";

$decodeValues = json_decode(dataCode);

$decodeValues->name; //this works now it has its value
#4

[eluser]kptengco[/eluser]
Sorry my bad.. i figured it out by using
Code:
fwrite
it on a file.

the data format was wrong when i open the file it display this data.
Code:
{\\\"SC_MC\\\":\\\"00000000\\\",\\\"SC_MID\\\":\\\"00000000\\\",\\\"SC_AMOUNT\\\":5000,\\\"SC_REF\\\":\\\"0000\\\"
,\\\"SC_PAYMODE\\\":\\\"Regular Installment\\\",\\\"SC_PAYTERM\\\":\\\"12\\\",\\\"SC_TRANSIP\\\":\\\"undefined\\\",\\\"SC_PAYREF\\\":\\\"000000000000\\\"
,\\\"SC_PAYDATE\\\":\\\"2012-05-21\\\",\\\"SC_REFERRER\\\":\\\"https:\/\/abc.abc.com\/abc\/abc\/abc.php\\\",\\\"SC_AGENT\\\":\\\"Mozilla
\/5.0 (Windows NT 6.1; rv:12.0) Gecko\/20100101 Firefox\/12.0\\\",\\\"SC_RRN\\\":null,\\\"SC_CUR_DATA\\\":\\\"PHP|1\\\"
,\\\"SC_ORIG_AMOUNT\\\":5000
,\\\"SC_STATUS\\\":\\\"approved\\\"}

Too many trailing backslashes that i can't decode it. But when im inserting it to my table there's no backslashes. Then i try to output it in a file and i know now the prob btw thnx to your feedback CroNiX Tongue
#5

[eluser]CroNiX[/eluser]
It's probably because you are using addslashes, which you shouldn't. If making your own insert query, use $this->db->escape($dataDecoded->name) to properly escape the variable.
#6

[eluser]kptengco[/eluser]
i didn't use any
Code:
addslashes
. Others said on php.net if u use json_encode and you use double quote php will automatically insert a backslash even thou you didn't put any backslashes unless you do this code snippet
Code:
json_encode($str,JSON_UNESCAPED_SLASHES)

Code:
$str = '{"name":"person 1"}';

json_encode($str);

The thing is im not the one who create the json data it just pass by my url and im the one who will decode it. So i don't have any idea that json data got a multiple backslashes. But, somethin' weird thou, i print it out
Code:
echo print_r($jsondata)
it prints without a backslashes. Anyways i use a
Code:
preg_replace
to remove all backslashes.

I guess cURL method or watever method use for payment processing datafeed or (ipn) instant payment notification then pass that json or post/get data onto my url. (ex. PayPal)
#7

[eluser]CroNiX[/eluser]
Kinda looked like you were using addslashes to me in post#2.

Code:
"INSERT INTO tbl (field1) VALUES('".addslashes($dataDecoded->name)." | ".$_REQUEST['jsonData']."')";
#8

[eluser]kptengco[/eluser]
oops sorry.. yea i did but i tried also not use it at first that was just a second test i add a
Code:
addslashes and a strip_tags also




Theme © iAndrew 2016 - Forum software by © MyBB