We have an array,
$a = array('a'=>NULL);
If we convert it to json with json_encode, it will return
echo json_encode($a, JSON_PRETTY_PRINT);
{
"a": null,
}
The value of key a
is null which would fail [NSDictionary writeToFile:atomically:] in Mac. Our best option till now is using array_map
with trim
:
echo json_encode(array_map(trim, $a), JSON_PRETTY_PRINT));
{
"a": "",
}
However, we need to update all json_encode and it looks clumpy. Is there any other option? We don't want to create a function for this simple purpose nor use override_function in adp
json_encode
does the right thing here. If some other function elsewhere cannot handlenull
, either fix/preprocess data for that function or do not give itnull
values. – deceze Jan 24 at 10:46array_walk()
your array with function checks is value isNULL
and turn it into""
? – s.webbandit Jan 24 at 10:47[NSDictionary writeToFile:..]
as it is pretty limited to objects supported by plist (for example you could useNSJSONSerialization
to read/write to file, or some open source JSON kit if app target is lower than OSX10.7/iOS5.0) – lupatus Jan 24 at 11:12