Use str_replace()
to accomplish this
echo str_replace("null",'"null"',CJSON::encode($cdata_provider->data));
Question
I write web services to be consumed in iPhone with iPhone developers. My co-developers from iPhone always complain that I send data as null instead of "null".
Let me explain this scenario in detail.
$cdata_provider = new CSqlDataProvider("Users");
echo CJSON::encode($cdata_provider->data);
OUTPUT
{
"users": [
{
"firstname":"Mike",
"lastname":null
},
{
"firstname":"Steve",
"lastname":null
}
]
}
The data is OK for me. But the iphone app crashes when it receives data as null. The iphone developer says that it is great if I send "null" (with double quotes) instead of null (without double quotes.
The required output would be:
{
"users": [
{
"firstname":"Mike",
"lastname":"null"
},
{
"firstname":"Steve",
"lastname":"null"
}
]
}
Can I accomplish this? Do I need to configure something? I did a lot google but didn't solve this issue.
Solution
Use str_replace()
to accomplish this
echo str_replace("null",'"null"',CJSON::encode($cdata_provider->data));
OTHER TIPS
Try to replace the null
with another word from the Query
$cdata_provider = new CSqlDataProvider("select firstname,replace(lastname,'null','MyWord') as lastname from YourUserTable");
echo CJSON::encode($cdata_provider->data);
Output will come as
{
"users":
[
{
"firstname":"Mike",
"lastname":"MyWord"
},
{
"firstname":"Steve",
"lastname":"MyWord"
}
]
}