CJSON::encode array

Hi,

now the CJSON::encode-function encode an associative array to a json-object.

i read the comments in the code and understand why this way was choosen.

But i think it would be better to create an array of objects with key and value as properties.

For example the following array



CJSON::encode (


	array(


		81523=>"87080",


		81528=>"87082",


		81533=>"87084",


	)


);


will be encoded to this:

{"81523":"87080","81528":"87082","81533":"87084"}

I think it would be clearer (and in my opinion better to use) if it look more like this:



	[


		{"key":"81523", "value":"87080"},


		{"key":"81528", "value":"87082"},


		{"key":"81533", "value":"87084"},


	]


What do you think?

Greetings from Germany

me23

Why don't you use something like the following?

CJSON::encode (


	array(


		array("key"=>81523, "value"=>87080),


		array("key"=>81528, "value"=>87082),


		array("key"=>81533, "value"=>87084)


	)


);

Hi juban,

thanks for your suggestion, but the array of the above example comes

from CHtml::listData() and i think the JSON::encode() should take the

effort for this manipulation from my shoulders :slight_smile:

Greetings

me23

CJSON is a generic JSON encoder. It has no knowledge whether or not to convert to the format that you suggested.

Ok, thanks qiang

Then what does CJSON do for me that json_encode() doesn’t? I too, was hoping it would work something like:




$response = CJSON::encode(Product::model()->findAll, 'id', 'title', 'description');



And it would kick back something like:




{

"root": [{

  "id" : "value of id' field",

  "title" : "value of title' field",

  "description" : "value of description' field"

  },{

  "id" : "value of id'' field",

  "title" : "value of title'' field",

  "description" : "value of description'' field"

  }]

}



Where id’ would be the first thing returned, id’’ the second… etc…

Maybe I’m being stupid and something like this already exists, but this would totally rock for plugging into a JSON store object in ExtJS. :slight_smile:

-C.

This is a good suggestion. I have a ticket for it assigned to me. Will implement it sooner or later.