ios - 使用 NSJSONSerialization 将 NSDictionary 转换为 NSData

标签 ios objective-c json

我有一个字典,如下所示

{
childViews =     (
            {
        childViews =             (
                            {
                childViews =                     (
                                            {
                        childViews =                             (
                                                            {
                                childViews =                                     (
                                );
                                properties =                                     {
                                    center = "NSPoint: {80.166666666666671, 20.166666666666668}";
                                };
                            }
                        );
                        properties =                             {
                            center = "NSPoint: {160, 110}";
                        };
                    },
                                            {
                        childViews =                             (
                                                            {
                                childViews =                                     (
                                );
                                properties =                                     {
                                    center = "NSPoint: {130, 20.166666666666668}";
                                };
                            }
                        );
                        properties =                             {
                            center = "NSPoint: {210, 160}";
                        };
                    },
                                            {
                        childViews =                             (
                                                            {
                                childViews =                                     (
                                );
                                properties =                                     {
                                    center = "NSPoint: {130, 20.166666666666668}";
                                };
                            }
                        );
                        properties =                             {
                            center = "NSPoint: {210, 210}";
                        };
                    },
                                            {
                        childViews =                             (
                                                            {
                                childViews =                                     (
                                );
                                properties =                                     {
                                    center = "NSPoint: {130, 20.166666666666668}";
                                };
                            }
                        );
                        properties =                             {
                            center = "NSPoint: {210, 260}";
                        };
                    },
                                            {
                        childViews =                             (
                                                            {
                                childViews =                                     (
                                );
                                properties =                                     {
                                    center = "NSPoint: {130, 20.166666666666668}";
                                };
                            }
                        );
                        properties =                             {
                            center = "NSPoint: {210, 260}";
                        };
                    },
                                            {
                        childViews =                             (
                                                            {
                                childViews =                                     (
                                );
                                properties =                                     {
                                    center = "NSPoint: {130.16666666666666, 20.166666666666668}";
                                };
                            }
                        );
                        properties =                             {
                            center = "NSPoint: {210, 310}";
                        };
                    },
                                            {
                        childViews =                             (
                                                            {
                                childViews =                                     (
                                );
                                properties =                                     {
                                    center = "NSPoint: {130, 20.166666666666668}";
                                };
                            }
                        );
                        properties =                             {
                            center = "NSPoint: {210, 360}";
                        };
                    },
                                            {
                        childViews =                             (
                                                            {
                                childViews =                                     (
                                );
                                properties =                                     {
                                    center = "NSPoint: {129.83333333333334, 20.166666666666668}";
                                };
                            }
                        );
                        properties =                             {
                            center = "NSPoint: {210, 410}";
                        };
                    },
                                            {
                        childViews =                             (
                                                            {
                                childViews =                                     (
                                                                            {
                                        childViews =                                             (
                                        );
                                        properties =                                             {
                                            center = "NSPoint: {0, 0}";
                                        };
                                    }
                                );
                                properties =                                     {
                                    center = "NSPoint: {207, 500.5}";
                                };
                            },
                                                            {
                                childViews =                                     (
                                );
                                properties =                                     {
                                    center = "NSPoint: {207, 671.83333333333326}";
                                };
                            },
                                                            {
                                childViews =                                     (
                                );
                                properties =                                     {
                                    center = "NSPoint: {409.83333333333337, 641}";
                                };
                            }
                        );
                        properties =                             {
                            center = "NSPoint: {217, 368}";
                        };
                    },
                                            {
                        childViews =                             (
                                                            {
                                childViews =                                     (
                                );
                                properties =                                     {
                                    center = "NSPoint: {70, 29.833333333333336}";
                                };
                            }
                        );
                        properties =                             {
                            center = "NSPoint: {80, 716}";
                        };
                    }
                );
                properties =                     {
                    center = "NSPoint: {207, 368}";
                };
            }
        );
        properties =             {
            center = "NSPoint: {207, 368}";
        };
    }
);
properties =     {
    center = "NSPoint: {207, 368}";
};

当我尝试使用代码将这个字典转换为 NSData 时

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[self toDictionary]
                                                   options:NSJSONWritingPrettyPrinted
                                                     error:&error];

我收到错误

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (NSConcreteValue)'

我需要帮助来理解可能导致这次崩溃的值(value)。

最佳答案

documentation 中所述,唯一可以通过 NSJSONSerialization 直接转换为 JSON 的类型是数组、字典、字符串、数字和空值。您的字典包含 NSValue 实例,这些实例包装了 NSPoint,它们不能转换为 JSON,因此是异常(exception)。您需要将它们替换为允许的类型之一,将点编码为两个值的数组、字典、字符串等。

从你的字典的描述中可能不清楚的原因是 NSValue 实例是用引号打印的,但它们实际上不是字符串——这只是它们的显示方式。

关于ios - 使用 NSJSONSerialization 将 NSDictionary 转换为 NSData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43581382/

相关文章:

ios - 日期格式化程序在 iOS 中给出错误的日期?

iphone - 有没有办法在 animateWithDuration 开始后暂停它?

ios - 提高 Tesseract OCR 质量失败

ios - 如何检测用户在 iOS 上授予麦克风权限?

ios - <file>-Info.plist 无法打开,因为没有这样的文件

javascript - 从 json 数组中删除一些数据

c# - Google Freebase Api C# .Net 示例

php - 为什么我无法将 CSV 文件中的新项目添加到 PHP 中 while 循环内的数组中?

ios - #defining objects 有什么开销?

ios - 不同模块中的相同类名