iOS JSON序列化用于基于NSObject的类 [英] iOS JSON serialization for NSObject-based classes

查看:103
本文介绍了iOS JSON序列化用于基于NSObject的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对自己的自定义类进行JSON序列化.我正在使用Objective-C/iOS5. 我想要执行以下操作:

I'd like to JSON-serialize my own custom classes. I'm working in Objective-C / iOS5. I'd like something to do the following:

Person* person = [self getPerson ]; // Any custom object, NOT based on NSDictionary
NSString* jsonRepresentation = [JsonWriter stringWithObject:person ];
Person* clone = [JsonReader objectFromJson: jsonRepresentation withClass:[Person Class]];

NSJSONSerialization(和其他一些库)似乎要求'person'类基于NSDictionary等.我想要一些可以序列化我想要定义的自定义对象的对象(在合理范围内).

It seems that NSJSONSerialization (and several other libraries) require the 'person' class to be based on NSDictionary etc. I want something that will serialize any custom object that I care to define (within reason).

让我们想象Person.h看起来像这样:

Let's imagine Person.h looks like this:

#import <Foundation/Foundation.h>
@interface Person : NSObject 
@property NSString* firstname;
@property NSString* surname;
@end

我希望为实例生成的JSON类似于以下内容:

I'd like the generated JSON for an instance to look similar to the following:

{"firstname":"Jenson","surname":"Button"}

我的应用程序使用ARC.我需要可以使用对象进行序列化和反序列化的东西.

My app uses ARC. I need something that will both serialise and deserialize using objects.

非常感谢.

推荐答案

这是一个棘手的问题,因为可以放入JSON的唯一数据是简单的简单对象(例如NSString,NSArray,NSNumber…),但不是自定义类或标量类型.为什么?如果不构建各种条件语句来将所有这些数据类型包装到这些对象类型中,则解决方案将是这样的:

This is a tricky one because the only data you can put into JSON are straight up simple objects (think NSString, NSArray, NSNumber…) but not custom classes or scalar types. Why? Without building all sorts of conditional statements to wrap all of those data types into those type of objects, a solution would be something like:

//at the top…
#import <objC/runtime.h>

    NSMutableDictionary *muteDictionary = [NSMutableDictionary dictionary];

    id YourClass = objc_getClass("YOURCLASSNAME");
    unsigned int outCount, i;
    objc_property_t *properties = class_copyPropertyList(YourClass, &outCount);
    for (i = 0; i < outCount; i++) {
        objc_property_t property = properties[i];
        NSString *propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
        SEL propertySelector = NSSelectorFromString(propertyName);
        if ([classInstance respondsToSelector:propertySelector]) {
            [muteDictionary setValue:[classInstance performSelector:propertySelector] forKey:propertyName];
        }
    }
    NSError *jsonError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:muteDictionary options:0 error:&jsonError];

这很棘手,尽管因为我之前说过.如果您有任何标量类型或自定义对象,那么整个过程就会崩溃.如果真的需要进行这样的事情,我建议您花些时间,并查看Ricard的链接,这些链接使您可以查看有助于将值包装到NSDictionary安全对象中的条件语句的属性类型.

This is tricky, though because of what I stated before. If you have any scalar types or custom objects, the whole thing comes tumbling down. If it's really critical to get something like this going, I'd suggest looking into investing the time and looking at Ricard's links which allow you to see property types which would assist on the conditional statements needed to wrap the values into NSDictionary-safe objects.

这篇关于iOS JSON序列化用于基于NSObject的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆