JSON model formatting issue

Hi All,

just having an issue with a basic web service I am developing using Yii. Fields that should be integers, are being output as strings.

For example, I have a table of countries, each with an integer id (and other integer fields). The problem is that the integer is being output as a string.

Example output:

{"id":"1","iso2":"AF","shortName":"Afghanistan","longName":"Islamic Republic of Afghanistan","iso3":"AFG","numcode":"004","un_member":"yes","calling_code":"93","cctld":".af"}

my controller list action looks like this:

$models = Country::model()->findAll();

$rows = array();

foreach($models as $model){

$rows[] = $model->attributes;

}

$this->_sendResponse(200, CJSON::encode($rows));

It seems that when model integer attributes are pulled, they get converted to strings. Is there any way around this? Or perhaps I am missing something simple?

thanks

I have the same problem, Does someone have the solution?

Thanks in advance!

I believe JSON attributes don’t have a concept of “type”, and all values will be quoted. If you want to use it as an int in your response, you can cast it as an int.

json_encode($data,JSON_NUMERIC_CHECK);