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

share|improve this question
json_encode does the right thing here. If some other function elsewhere cannot handle null, either fix/preprocess data for that function or do not give it null values. – deceze Jan 24 at 10:46
Why don't you just array_walk() your array with function checks is value is NULL and turn it into ""? – s.webbandit Jan 24 at 10:47
You can make the changes in your php scripts and all other possible/future json sources ...or fix it on the other side where you try to presist the NSDictionary, see stackoverflow.com/questions/8075147/… – VolkerK Jan 24 at 11:11
1  
maybe you should rather consider using something else than [NSDictionary writeToFile:..] as it is pretty limited to objects supported by plist (for example you could use NSJSONSerialization 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
1  
@s.webbandit array_map(trim, $a) can do the same, and it helps me to trim the array too. – neo.orz.hk Jan 24 at 14:12
show 1 more comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.