问题
I have serialized a string in PHP using serialize method
$serializedResult = serialize($value);
How to unserialize this serialized string in Objective-C
回答1:
I don't think there is a serialized PHP object parser for Objective C.
However you could use JSON instead. On the PHP side use json_encode
, and on the Objective C side see: http://blog.zachwaugh.com/post/309924609/how-to-use-json-in-cocoaobjective-c
Or as Tommy pointed out the now native implementation: http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html
回答2:
Same problem occur to me. As solution i wrote parser to do this job. It have both serialization and deserialization functions.You can get it from this link.
This is the testing example for serialization, RTSerializer *serializer=[[RTSerializer alloc]init];
NSMutableDictionary *pack=[NSMutableDictionary dictionary];
[pack setValue:@"id" forKey:@"3"];
[pack setValue:@"tariffid" forKey:@"152"];
[pack setValue:@"rgid" forKey:@"1"];
[pack setValue:@"name" forKey:@"Test"];
NSString *_data=@"";
_data=[serializer pack inString:_data];
example for desrialization,
id object=[serializer deserialize:@"a:1:{i:0;a:4:{s:2:\"id\";s:1:\"3\";s:8:\"tariffid\";s:3:\"152\";s:4:\"rgid\";s:1:\"1\";s:4:\"name\";s:4:\"Test\";}}"];
回答3:
Don't think this is possible. According to PHP manual serialize method is supposed to be used only inside PHP scripts.
You should use some open formats, like JSON. Or you can create your own "serialize" implementation in both languages.
回答4:
Take a look on specification and realizations - http://objectmix.com/php/362009-specification-serialize.html#post1335166 And here look sources of var_unserializer.c from PHP - http://svn.php.net/repository/php/php-src/branches/PHP_5_2/ext/standard/var_unserializer.c
来源:https://stackoverflow.com/questions/12136119/php-serialize-unserialize-like-method-in-objective-c