将 NSDictionary 对象转换为自定义复杂对象

发布于 2024-11-05 22:21:55 字数 271 浏览 2 评论 0原文

我需要将 JSON 字符串反序列化为自定义复杂对象。

例如,假设我有 json 字符串:

{"Menu": {
"categoryList": {
"Category": [
{"name": "Cat1"},
{"name": "Cat1"},
{"name": "Cat3"}
]
}
}}

如何反序列化该字符串以初始化具有 CategoryList 的 Menu 对象,其中包含 3 个 Category 类类型的类别对象?有什么办法吗?

I need to deserialize JSON string to custom complex objects.

For example lets say I have the json string:

{"Menu": {
"categoryList": {
"Category": [
{"name": "Cat1"},
{"name": "Cat1"},
{"name": "Cat3"}
]
}
}}

How can I deserialize this string to initialize a Menu object that has a categoryList which includes 3 category objects of type Category class? Is there any way to to this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我要还你自由 2024-11-12 22:21:55

这是一项必需的功能,但似乎不存在良好的(公共)解决方案。

This is a needed capability for which there does not seem to exist a good (public) solution.

澉约 2024-11-12 22:21:55

尝试使用 JSON 解析器。

http://code.google.com/p/json-framework/

它将分析你的字符串并返回一个代表你的数据的 NSObject (NSArray 或 NSDictionary)。

编辑:

嗯,由于OP想要获得一个自定义对象而不是NSDictionary/NSArray,它可以实现如下(假设困难将获得正确的数据并设置每个新的对象属性)

基于代码提供者@andrewsardone,在使用更适合您的项目的任何解决方案处理 JSON 解析之后,可以使用 KVO 轻松实现一个函数来获取具有相应属性设置的新对象

+(id) objectFromDictionary:(NSDictionary *)dict {

    id entry = [[self alloc] init];

    Class aClass = [entry class];

    do {

        unsigned int outCount, i;
        objc_property_t *properties = class_copyPropertyList(aClass, &outCount);
        for (i = 0; i < outCount; i++) {
            objc_property_t property = properties[i];
            NSString *propertyName = [[[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding] autorelease];
            id propertyValue = [dict objectForKey:propertyName];
            if (propertyValue && ![propertyValue isEqual:[NSNull null]]) {
                [entry setValue:propertyValue forKey:propertyName];
            }
        } 

        free(properties);

        //added to take care of the class inheritance
        aClass = [aClass superclass];

    } while (![[[aClass class] description] isEqualToString:[NSObject description]]);

    return [entry autorelease];
}

Try using a JSON parser.

http://code.google.com/p/json-framework/

It will analyse your string and give you back an NSObject (NSArray or NSDictionary) that represents your data.

EDIT:

Well, as the OP wanted to get a custom object instead of an NSDictionary/NSArray, it could be implemented something as the following (assuming that the dificulty would get the correct data and set each of the new object properties)

Based on the code provided by @andrewsardone, one could easily implement a function using KVO to get a new object with the properties set accordingly, after handling the JSON parsing with whatever solution fits your project better

+(id) objectFromDictionary:(NSDictionary *)dict {

    id entry = [[self alloc] init];

    Class aClass = [entry class];

    do {

        unsigned int outCount, i;
        objc_property_t *properties = class_copyPropertyList(aClass, &outCount);
        for (i = 0; i < outCount; i++) {
            objc_property_t property = properties[i];
            NSString *propertyName = [[[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding] autorelease];
            id propertyValue = [dict objectForKey:propertyName];
            if (propertyValue && ![propertyValue isEqual:[NSNull null]]) {
                [entry setValue:propertyValue forKey:propertyName];
            }
        } 

        free(properties);

        //added to take care of the class inheritance
        aClass = [aClass superclass];

    } while (![[[aClass class] description] isEqualToString:[NSObject description]]);

    return [entry autorelease];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文