【问题标题】:Problems serializing a dictionary with TouchJson使用 TouchJson 序列化字典的问题
【发布时间】:2011-03-23 10:22:42
【问题描述】:

我正在为 ipad 开发一个小应用程序,我正在尝试将字典序列化为 NSData 以保存在磁盘中。我正在使用框架 TouchJson。 以及我的示例结构示例:

{
line =     {
    78986928 =         (
        "NSPoint: {442, 266}",
        (...)
        "NSPoint: {370, 634}"
    );
};

}

我的字典的结构是:一个里面有字典的字典。这个字典有一个字符串 (ID) 和一个带有 NSValue 的 NSMutableArray。

我正在使用的代码行是:

NSData *jsonData = [[CJSONSerializer serializer] serializeObject:templates error:&error];

变量error给我的错误是:

2011-03-23 10:12:12.957 GestureFramework[286:207] Error Domain=TODO_DOMAIN Code=-1 "Could not serialize object '{
line =     {
    78986928 =         (
        "NSPoint: {442, 266}",
        (...)
        "NSPoint: {370, 634}"
    );
};
}'" UserInfo=0x4e27aa0 {NSLocalizedDescription=Could not serialize object '{
    line =     {
        78986928 =         (
            "NSPoint: {442, 266}",
            (...)
            "NSPoint: {370, 634}"
        );
    };
}'}

提前谢谢

【问题讨论】:

    标签: iphone json ios ipad touchjson


    【解决方案1】:

    TouchJSON 支持序列化以下类型:

    • NSNull
    • NSNumber
    • NSString
    • NSArray
    • NSDictionary
    • NSData

    如果您想序列化其他类型,则需要实现 -(NSData*)JSONDataRepresentation(在子类或类别上)。

    这是我用于NSDate 的示例:

    @interface NSDate (JSONDataRepresentation)
    - (NSData*)JSONDataRepresentation; 
    @end
    

    .

    @implementation NSDate (JSONDataRepresentation)
    - (NSData*)JSONDataRepresentation 
    {
        return [@"\"didn't want to waste the space to do the real conversion\"" dataUsingEncoding: NSUTF8StringEncoding]; 
    }
    @end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多