Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use json_decode($content, true) to decode json string to php array. I found null value has been converted to white space. Then I manage it through
if ($cell=='')
{
$values .= 'Null,';
}

But I found that son_decode removes zero also. How do I get zero as zero and null as null from json_decode. Any help?

What I have tried:

json_decode($content, true)

if ($cell=='')
{
$values .= 'Null,';
}
Posted
Updated 5-Nov-16 20:00pm
v2
Comments
Suvendu Shekhar Giri 17-Oct-16 11:00am    
Can you show the input?
Make sure that it is UTF-8 encoded data.
Member 10521029 28-Oct-16 11:44am    
I used the following method to encode in c#. I fill datatable from ms sql server. How can I confirm UTF-8? Please help.

private static string DataTableToJson(DataTable dataTable)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
var rows = (from DataRow d in dataTable.Rows
select dataTable.Columns.Cast<datacolumn>().ToDictionary(col => col.ColumnName, col => d[col])).ToList();

return serializer.Serialize(rows);
}
Member 10521029 18-Oct-16 13:41pm    
I have checked the value is zero after serialized to json string. It is removed or replaced to white space after decode it to php array. Thank you

1 solution

C#
I worked it out. There is no encoding issue. It was in PHP array. PHP array returns empty for Null value. var_dump() returns Null value as Null but print_r() returns empty. I used the following code to get Null value as Null. 

if ($cell==Null)
{
 $values .= 'Null,';
} 

But PHP interpret 0 and false as Null also. So the above code returns 0 and false as null also. Here I changed it as below and got it worked.

if ($cell===Null)
{
 $values .= 'Null,';
} 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900