CJSON::encode force Object?

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?


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); ?>


worth a try!

let me test it tomorrow :)

I try it today, my model array is ok in PHP, but after CJSON::encode, they’ll be nothing but { }


I just extend CJSON and overwrite “encode” and “nameValue” method, it works :)