pizwu
(Pizwu000)
March 14, 2012, 9:38am
1
I like CJOSN::encode, but I want it encode my hash & array both into object.
CJSON::encode will determine which is array and which is hash, and encode to array or object separately
sometimes, my array in PHP will have continues index from 0, and be treated as array
but sometimes, my array in PHP doesn’t have continues index from 0, and be treated as hash
the client coding now have to check whether is hash or array, that’s troublesome
any suggestion?
[update]
I mean I want a feature like json_encode in PHP, which can pass JSON_FORCE_OBJECT
that will force my array translate into object.
Never used CJSON, but would typecasting work?
dan at elearnapp dot com 13-Jan-2011 12:01
If you need to force an object (ex: empty array) you can also do:
<?php json_encode( (object)$arr ); ?>
which acts the same as
<?php json_encode($arr, JSON_FORCE_OBJECT); ?>
http://www.php.net/manual/en/function.json-encode.php#101843
I like CJOSN::encode, but I want it encode my hash & array both into object.
CJSON::encode will determine which is array and which is hash, and encode to array or object separately
sometimes, my array in PHP will have continues index from 0, and be treated as array
but sometimes, my array in PHP doesn’t have continues index from 0, and be treated as hash
the client coding now have to check whether is hash or array, that’s troublesome
any suggestion?
[update]
I mean I want a feature like json_encode in PHP, which can pass JSON_FORCE_OBJECT
that will force my array translate into object.
pizwu
(Pizwu000)
March 15, 2012, 1:47am
4
I try it today, my model array is ok in PHP, but after CJSON::encode, they’ll be nothing but { }
sigh…
worth a try!
let me test it tomorrow
pizwu
(Pizwu000)
March 15, 2012, 2:29am
5
I just extend CJSON and overwrite “encode” and “nameValue” method, it works