Pok*_*com 33 json couchdb objective-c nsdictionary nsarray
我需要将objective-c对象序列化和反序列化为JSON以存储在CouchDB中.人们是否有任何示例代码用于一般解决方案的最佳实践?我查看了一些JSON框架,它们在NSDictionary/NSArray级别停止.即很多框架将NSDictionary/NSArray序列化和反序列化为JSON.但我仍然需要将NSDictionary转换为Objective-C对象.
为了使事情变得更复杂,我的对象A可以引用对象B的NSArray/NSDictionary.
我的问题与这个问题非常相似,增加了收集要求.
X S*_*ham 37
最后,我们可以使用JSONModel轻松解决此问题.这是目前为止最好的方法.JSONModel是一个基于Class一般序列化/反序列化对象的库.你甚至可以使用基于财产像非NSObject的int,short和float.它还可以满足嵌套复杂的JSON.
考虑这个JSON示例:
{ "accounting" : [{ "firstName" : "John",
"lastName" : "Doe",
"age" : 23 },
{ "firstName" : "Mary",
"lastName" : "Smith",
"age" : 32 }
],
"sales" : [{ "firstName" : "Sally",
"lastName" : "Green",
"age" : 27 },
{ "firstName" : "Jim",
"lastName" : "Galley",
"age" : 41 }
]}
Run Code Online (Sandbox Code Playgroud)
1)反序列化示例.在头文件中:
#import "JSONModel.h"
@interface Person : JSONModel
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;
@property (nonatomic, strong) NSNumber *age;
@end
@protocol Person;
@interface Department : JSONModel
@property (nonatomic, strong) NSMutableArray<Person> *accounting;
@property (nonatomic, strong) NSMutableArray<Person> *sales;
@end
Run Code Online (Sandbox Code Playgroud)
在实施文件中:
#import "JSONModelLib.h"
#import "myJSONClass.h"
NSString *responseJSON = /*from example*/;
Department *department = [[Department alloc] initWithString:responseJSON error:&err];
if (!err)
{
for (Person *person in department.accounting) {
NSLog(@"%@", person.firstName);
NSLog(@"%@", person.lastName);
NSLog(@"%@", person.age);
}
for (Person *person in department.sales) {
NSLog(@"%@", person.firstName);
NSLog(@"%@", person.lastName);
NSLog(@"%@", person.age);
}
}
Run Code Online (Sandbox Code Playgroud)
2)序列化示例.在实现文件中:
#import "JSONModelLib.h"
#import "myJSONClass.h"
Department *department = [[Department alloc] init];
Person *personAcc1 = [[Person alloc] init];
personAcc1.firstName = @"Uee";
personAcc1.lastName = @"Bae";
personAcc1.age = [NSNumber numberWithInt:22];
[department.accounting addOject:personAcc1];
Person *personSales1 = [[Person alloc] init];
personSales1.firstName = @"Sara";
personSales1.lastName = @"Jung";
personSales1.age = [NSNumber numberWithInt:20];
[department.sales addOject:personSales1];
NSLog(@"%@", [department toJSONString]);
Run Code Online (Sandbox Code Playgroud)
这是来自Serialize示例的NSLog结果:
{ "accounting" : [{ "firstName" : "Uee",
"lastName" : "Bae",
"age" : 22 }
],
"sales" : [{ "firstName" : "Sara",
"lastName" : "Jung",
"age" : 20 }
]}
Run Code Online (Sandbox Code Playgroud)
Jen*_*fke 10
听起来您正在寻找一个序列化库,它可以让您将自己的自定义类的对象转换为JSON,然后重新构建它们.属性列表类型的序列化(NSArray,NSNumber等)已存在于第三方库中,甚至内置于OS X 10.7和iOS 5中.
所以,我认为答案基本上是"不".我在一个或两个月前在cocoa-dev邮件列表上问过这个确切的问题,而我最接近的是来自Mike Abdullah,他指的是他写的一个实验性图书馆:
https://github.com/mikeabdullah/KSPropertyListEncoder
这会将对象归档到内存中的属性列表,但正如我所说,已经存在用于将这些转换为JSON的API.
还有一个名为Objectify的商业应用程序声称可以做类似的事情:
http://tigerbears.com/objectify/
我有可能最终实现你所要求的作为我的CouchCocoa库的一部分,但我还没有潜入这项任务.
https://github.com/couchbaselabs/CouchCocoa
在NSDictionary,NSArray和NSJSONSerialization的帮助下,您可以轻松地将JSON功能添加到NSObject类
只要看一下这个例子就很容易理解了.
将JSON功能添加到NSObject类: -
@interface JsonClassEmp : NSObject
@property(strong,nonatomic)NSString *EmpName,*EmpCode;
-(NSDictionary*)GetJsonDict;
@end
@implementation JsonClassEmp
@synthesize EmpName,EmpCode;
//Add all the properties of the class in it.
-(NSDictionary*)GetJsonDict
{
return [NSDictionary dictionaryWithObjectsAndKeys:EmpName,@"EmpName",EmpCode,@"EmpCode", nil];
}
@end
Run Code Online (Sandbox Code Playgroud)
JSON字符串生成器: -
在iOS 5中,Apple引入了NSJSONSerialization,用于解析JSON字符串,因此使用它我们将生成JSON字符串.
-(NSString*)GetJSON:(id)object
{
NSError *writeError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted error:&writeError];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
return jsonString;
}
Run Code Online (Sandbox Code Playgroud)
转向Apple的实施总是更安全,因为您可以保证它将得到维护并保持最新.
使用方法: -
- (void)viewDidLoad
{
[super viewDidLoad];
JsonClassEmp *emp1=[[JsonClassEmp alloc]init];
[emp1 setEmpName:@"Name1"];
[emp1 setEmpCode:@"1"];
JsonClassEmp *emp2=[[JsonClassEmp alloc]init];
[emp2 setEmpName:@"Name2"];
[emp2 setEmpCode:@"2"];
//Add the NSDictionaries of the instances in NSArray
NSArray *arrEmps_Json=@[emp1.GetJsonDict,emp2.GetJsonDict];
NSLog(@"JSON Output: %@", [self GetJSON:arrEmps_Json]);
}
Run Code Online (Sandbox Code Playgroud)
通常的方法是将反序列化的数据导入NSDictionary或NSArray,然后将其分配给类属性.
我确信使用上面使用的方法和思想可以轻松地序列化和反序列化复杂的json.
| 归档时间: |
|
| 查看次数: |
59702 次 |
| 最近记录: |