问题
Does anyone know if there's any support in Realm (iOS) to be able to serialise an RLMObject into JSON for export?
回答1:
I don't believe there is any support officially, but this article may describe some other third party libraries which may be able to achieve what you are after
http://blog.matthewcheok.com/working-with-realm/
回答2:
It should work like with any other objects:
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:realmObject options:NSJSONWritingPrettyPrinted error:&writeError];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
But:
An object that may be converted to JSON must have the following properties:
The top level object is an NSArray or NSDictionary.
All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.
All dictionary keys are instances of NSString.
Numbers are not NaN or infinity.
https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/index.html]
来源:https://stackoverflow.com/questions/29940658/is-it-possible-to-serialise-a-realm-object-into-json