使用NSJSONSerialization将NSDictionary转换为NSData

时间:2017-04-24 06:41:04

标签: 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)'

我需要帮助来理解可能导致此崩溃的价值。

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

JSON是一个包含数组,字典(" javascript中的对象"),字符串,数字和null(NSNUll)的结构。您正在尝试将NSPoint编码为JSON,而JSON不是为此而设计的。我建议将点转换为字符串,或者转换为对象(类似于:{x:160,y:110})。

另请参阅https://developer.apple.com/reference/foundation/jsonserialization以获取有关哪些对象可以转换为JSON的完整说明。