toJSONString() method not working with json_decode

164 views
Skip to first unread message

rjha94

unread,
Feb 26, 2007, 7:13:27 AM2/26/07
to php-json
Hi Folks
I am trying to create and post JSON data to a php script. From the
client I create a javascript object, use toJSONString() function on
it (part of json.js file taken from http://www.json.org/json.js) and
post the stringified value as a $_POST variable.

Problem is , json_decode does not seem to parse anything out of it. I
will include the complete example later in this mail. But the same
code/methodology is working for me with both perl as well as ruby
libraries. so I do not think anything is amiss on client side.

Can anyone help me out? Now i am at a stage when you start tearing the
hair out ;o(

Here is the complete example

on client side
=========================
<form id="searchForm" name="searchForm" action="<?php
$_SERVER['PHP_SELF'] ?>" method="POST">
<input type="text" name="search_token" size="40"/> </td>
<input type="button" value="submit" onClick="search();"/>
<input type="hidden" name="filter" value=""/>
</form>

javascript search function
=======================

function search(){
filterObj = {};
var sform = document.getElementById('searchForm');
var token = sform.search_token.value ;
filterObj.token = token ;
sform.filter.value = filterObj.toJSONString() ;
sform.submit();
}

PHP script
==============
$strFilter = $_POST['filter'];
echo " filter string == $strFilter \n ";
$filter = json_decode($strFilter);
$token = $filter->token ;
echo " from post :: token is:: $token \n ";

And this outputs

filter string == {"token":"koko"} from post :: token is::
so decode is not going fine, I expect to see what the user typed in.

Thanks

- Rajeev.

Omar Kilani

unread,
Feb 26, 2007, 8:01:30 AM2/26/07
to php-...@googlegroups.com
Hey Rajeev,

On 2/26/07, rjha94 <jha.r...@gmail.com> wrote:
>
> Hi Folks
> I am trying to create and post JSON data to a php script. From the
> client I create a javascript object, use toJSONString() function on
> it (part of json.js file taken from http://www.json.org/json.js) and
> post the stringified value as a $_POST variable.
>
> Problem is , json_decode does not seem to parse anything out of it. I
> will include the complete example later in this mail. But the same
> code/methodology is working for me with both perl as well as ruby
> libraries. so I do not think anything is amiss on client side.
>

> PHP script
> ==============
> $strFilter = $_POST['filter'];
> echo " filter string == $strFilter \n ";
> $filter = json_decode($strFilter);
> $token = $filter->token ;
> echo " from post :: token is:: $token \n ";
>
> And this outputs
>
> filter string == {"token":"koko"} from post :: token is::
> so decode is not going fine, I expect to see what the user typed in.

Unfortunately, I'm not able to reproduce what you're seeing...

phpsh (c)2006 by Charlie Cheever and Dan Corson and Facebook, Inc.
type 'h' or 'help' to see instructions & features
Commandline: php -q /opt/phpsh/phpsh.php

php> debug_zval_dump(json_decode('{"token":"koko"}'));
object(stdClass)#2 (1) refcount(1){
["token"]=>
string(4) "koko" refcount(1)
}

php> $filter = json_decode('{"token":"koko"}');
php> echo $filter->token;
koko

On Linux / php 5.1.6 / php-json 1.2.1.

Can you please provide more details about your environment, such as
OS, PHP version, php-json version, if you have any "weird" php.ini
values on (like magic quotes) and so on? :)

Is it possible to get the output of debug_zval_dump on the return
value of your json_decode call? And possibly on $_POST as well?

Thanks! :)

> Thanks
>
> - Rajeev.

Regards,
Omar

rjha94

unread,
Mar 1, 2007, 12:56:37 PM3/1/07
to php-json
debug_zval_dump is printing NULL for json_decode($strFilter) so not
much help there.

I have PHP/5.2.1 on win 32 (XP). build 2600. JSON version 1.2.1

One difference that I noticed is the json_decode strings in all
examples are of type '{"x":"y , that is they are surrounded by single-
quotes. whereas the filter string i get from json.js toJsonString() is
without single quotes before and after . I tried adding single quotes
to my filter string manually but that also did not help.

I can work around this but it is of course one of those things in life
that just piques you :(

Thanks

- Rajeev

On Feb 26, 6:01 pm, "Omar Kilani" <omar.kil...@gmail.com> wrote:
> Hey Rajeev,
>

> On 2/26/07, rjha94 <jha.raj...@gmail.com> wrote:
>
>
>
>
>
> > Hi Folks
> > I am trying to create and post JSON data to a php script. From the
> > client I create a javascript object, use toJSONString() function on

> > it (part of json.js file taken fromhttp://www.json.org/json.js) and

rjha94

unread,
Mar 4, 2007, 12:21:26 AM3/4/07
to php-json
ok let me go on record and say it was my own stupidity/bug. I pass all
POST tokens through a function to strip them of special chars etc
function htmlsecure($x){
return trim(htmlspecialchars($x,ENT_QUOTES));
}
when I take the filter tokens from POST w/o any processing everything
works fine ;o)
Reply all
Reply to author
Forward
0 new messages