Is it possible to serialise a Realm Object into JSON?

情到浓时终转凉″ 提交于 2019-12-08 03:54:33

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!