将iOS objective c对象转换为JSON字符串
我有一个客观的C类,比如将iOS objective c对象转换为JSON字符串,ios,json,serialization,nsarray,Ios,Json,Serialization,Nsarray,我有一个客观的C类,比如 @interface message : NSObject { NSString *from; NSString *date; NSString *msg; } 我有一个NSMutableArray这个消息类的实例。我希望使用iOS5SDK中新的JSONSerialization API将NSMutableArray中的所有实例序列化为JSON文件。我该怎么做 是否通过遍历NSArray中元素的每个实例来创建每个键的NSDictionary?有人能帮我解决这个问
@interface message : NSObject {
NSString *from;
NSString *date;
NSString *msg;
}
我有一个NSMutableArray这个消息类的实例。我希望使用iOS5SDK中新的JSONSerialization API将NSMutableArray中的所有实例序列化为JSON文件。我该怎么做
是否通过遍历NSArray中元素的每个实例来创建每个键的NSDictionary?有人能帮我解决这个问题吗?我无法在Google中获得好的结果,因为“JSON”将结果倾斜到服务器端调用和数据传输,而不是序列化。非常感谢
编辑:
编辑:我制作了一个虚拟应用程序,应该是你的一个好例子 我从您的代码片段创建了一个消息类
//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中设置了一个包含两条消息的NSArray。诀窍在于,不仅顶级对象(在您的例子中是通知)需要序列化,通知包含的所有元素也需要序列化:这就是我在Message类中创建dictionary方法的原因
//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
因此,我运行应用程序时的输出为:
2012-05-11 11:58:36.018堆栈[3146:f803]JSON输出:[
{
“msg”:“c”,
“发件人”:“a”,
“日期”:“b”
},
{
“msg”:“f”,
“发件人”:“d”,
“日期”:“e”
}
]
原始答复:
是您要查找的文档吗?现在您可以轻松地使用。JSONModel是一个基于类对对象进行一般序列化/反序列化的库。您甚至可以使用非基于nsobject的属性,如
int
、short
和float
。它还可以处理嵌套的复杂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]);
序列化示例。在实现文件中:
#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]);
注意:这仅适用于可序列化对象。上述答案是在对问题本身的编辑中提供的,但我自己总是在“答案”部分寻找答案;-)
这是我在项目中使用的一个库,它可以帮助您轻松地将json字符串与数据模型匹配起来,只需一行代码
...
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];
...
OP首先需要将其对象转换为一个字典,但这非常简单(静态方法就可以了)Damo:该页面没有解释如何将自定义类数组转换为JSON。这就是我正在努力寻找的。如果你所有的类IVAR都是NSString,那么它应该会神奇地工作。。。。警告:我尚未尝试此操作。@Damo我遇到运行时错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[message dictionaryRepresentation]:无法识别的选择器已发送到instance@Damon错误*writeError=nil;NSData*jsonData=[NSJSONSerialization dataWithJSONObject:通知选项:NSJSONWriting预打印错误:&writeError];NSString*jsonString=[[NSString alloc]initWithData:jsonData编码:NSUTF8StringEncoding];NSLog(@“JSON输出:%@”,jsonString);通知是消息对象hanks的NSMutableArray!这个库非常有用。如果我们想使用JSONModel序列化消息数组呢?嗨。如何转换NSObject的数组。谢谢:)嘿@kemdo-不确定-我不再做ios开发了,所以不能说。。。。祝你好运