【问题标题】:iPhone obj c- Convert NSMutableArray of custom objects to JSONiPhone obj c-将自定义对象的 NSMutableArray 转换为 JSON
【发布时间】:2014-01-23 18:54:09
【问题描述】:

我有以下两个类:

// Animal.h
@interface Animal : NSObject {
    NSString *name;
    NSString *description;
}

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *description;

@end

// Group.h
@interface Group : NSObject {
    NSMutableArray<Animal> *list;
}

@property (nonatomic, retain) NSMutableArray<Animal> *list;

@end

我想将包含所有 Group 信息的 NSString 转换为 Group 对象,将新的 Animal 对象添加到 NSMutableArray 中并再次将其转换为字符串以将其发送回服务器。

这应该很简单,但我无法找到序列化/反序列化对象的方法。我尝试使用 JSNModel,但无法正常工作。

有人可以帮帮我吗? 提前致谢!

【问题讨论】:

    标签: iphone objective-c json serialization nsmutablearray


    【解决方案1】:

    使用 SBJson 框架:https://github.com/SBJson/SBJson

    #import "SBJson.h"
    SBJsonWriter *writer = [[SBJsonWriter alloc] init];
    NSString *jsonString = [writer stringWithObject:arr];
    

    您将获得一个 JSON 字符串。应该没问题。

    【讨论】:

    • 谢谢,但在这种情况下,jsonString 为 nil。我的对象“arr”是一个 NSObject,对吗?
    猜你喜欢
    • 1970-01-01
    • 2011-01-15
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多