将iOS目标c对象转换为JSONstring

我有一个客观的C类,

@interface message : NSObject { NSString *from; NSString *date; NSString *msg; } 

我有这个消息类的实例的NSMutableArray。 我想使用iOS 5 SDK中的新JSONSerialization API将NSMutableArray中的所有实例序列化为JSON文件。 我怎样才能做到这一点 ?

是通过遍历NSArray中的每个元素的实例来创build每个键的NSDictionary? 有人可以帮助如何解决这个问题的代码? 我无法在Google中取得好成绩,因为“JSON”会将结果向服务器端调用和数据传输倾斜,而不是序列化。 非常感谢。

编辑:

 NSError *writeError = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:notifications options:NSJSONWritingPrettyPrinted error:&writeError]; NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSLog(@"JSON Output: %@", jsonString); 

编辑:我做了一个虚拟的应用程序,应该是一个很好的例子给你。

我从你的代码片段创build一个消息类;

 //Message.h @interface Message : NSObject { NSString *from_; NSString *date_; NSString *msg_; } @property (nonatomic, retain) NSString *from; @property (nonatomic, retain) NSString *date; @property (nonatomic, retain) NSString *msg; -(NSDictionary *)dictionary; @end //Message.m #import "Message.h" @implementation Message @synthesize from = from_; @synthesize date = date_; @synthesize msg = mesg_; -(void) dealloc { self.from = nil; self.date = nil; self.msg = nil; [super dealloc]; } -(NSDictionary *)dictionary { return [NSDictionary dictionaryWithObjectsAndKeys:self.from,@"from",self.date, @"date",self.msg, @"msg", nil]; } 

然后,我在AppDelegate中build立了两条消息的NSArray。 诀窍是,不仅顶级对象(在你的情况下,通知)需要可序列化,但通知所包含的所有元素也是如此:这就是为什么我在Message类中创build了字典方法。

 //AppDelegate.m ... Message* message1 = [[Message alloc] init]; Message* message2 = [[Message alloc] init]; message1.from = @"a"; message1.date = @"b"; message1.msg = @"c"; message2.from = @"d"; message2.date = @"e"; message2.msg = @"f"; NSArray* notifications = [NSArray arrayWithObjects:message1.dictionary, message2.dictionary, nil]; [message1 release]; [message2 release]; NSError *writeError = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:notifications options:NSJSONWritingPrettyPrinted error:&writeError]; NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSLog(@"JSON Output: %@", jsonString); @end 

我运行应用程序时的输出是这样的:

JSON输出:[{“msg”:“c”,“from”:“a”,“date”:“b”},{“msg” :“f”,“from”:“d”,“date”:“e”}]

原文答案:

这是你正在寻找的文件?

现在你可以使用JSONModel轻松解决这个问题。 JSONModel是一个通用的基于Class的序列化/反序列化对象的库。 你甚至可以使用基于非int shortfloat等属性。 它也可以提供嵌套复杂的JSON。 它处理错误检查你。

反序列化示例 。 在头文件中:

 #import "JSONModel.h" @interface Message : JSONModel @property (nonatomic, strong) NSString* from; @property (nonatomic, strong) NSString* date; @property (nonatomic, strong) NSString* message; @end 

在执行文件中:

 #import "JSONModelLib.h" #import "yourPersonClass.h" NSString *responseJSON = /*from somewhere*/; Message *message = [[Message alloc] initWithString:responseJSON error:&err]; if (!err) { NSLog(@"%@ %@ %@", message.from, message.date, message.message): } 

序列化示例 。 在执行文件中:

 #import "JSONModelLib.h" #import "yourPersonClass.h" Message *message = [[Message alloc] init]; message.from = @"JSON beast"; message.date = @"2012"; message.message = @"This is the best method available so far"; NSLog(@"%@", [person toJSONString]); 

注意:这只适用于可序列化的对象。 这个答案在上面提供了对问题本身的编辑,但我总是在“答案”部分自己寻找答案;-)

 - (NSString*) convertObjectToJson:(NSObject*) object { NSError *writeError = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted error:&writeError]; NSString *result = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; return result; } 

下面是我在我的项目BWJSONMatcher中使用的一个库,它可以帮助您轻松地将您的jsonstring与您的数据模型进行匹配,而不超过一行代码。

 ... NSString *jsonString = @"{your-json-string}"; YourValueObject *dataModel = [YourValueObject fromJSONString:jsonString]; NSDictionary *jsonObject = @{your-json-object}; YourValueObject *dataModel = [YourValueObject fromJSONObject:jsonObject]; ... YourValueObject *dataModel = instance-of-your-value-object; NSString *jsonString = [dataModel toJSONString]; NSDictionary *jsonObject = [dataModel toJSONObject]; ...