简体   繁体   English

使用NSJSONSerialization将NSDictionary转换为NSData

[英]Converting NSDictionary to NSData using NSJSONSerialization

I have a dictionary that looks like below 我有一本如下的字典

{
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}";
};

} }

When I try to convert this dictionary into NSData using code 当我尝试使用代码将此字典转换为NSData时

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

I get error 我得到错误

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

I need help in understanding the value that might be causing this crash. 我需要帮助来了解可能导致此崩溃的值。

As mentioned in the documentation , the only types which are directly convertible to JSON through NSJSONSerialization are arrays, dictionaries, strings, numbers, and null values. 文档中所述,只能通过NSJSONSerialization直接转换为JSON的NSJSONSerialization是数组,字典,字符串,数字和null值。 Your dictionaries contain NSValue instances which wrap NSPoint s, which are not convertible to JSON, thus the exception. 您的词典中包含包装NSPointNSValue实例,这些实例不可转换为JSON,因此是例外。 You will need to replace those with one of the allowed types, either encoding points as an array of two values, a dictionary, a string, etc. 您将需要用允许的类型之一替换那些类型,将编码点编码为两个值,字典,字符串等的数组。

The reason this may not be clear from the description of your dictionary is that the NSValue instances are printed in quotes, but they are not actually strings — that is simply how they display. 从字典的描述中可能不清楚这一点的原因是NSValue实例是用引号打印的,但它们实际上不是字符串,这只是它们的显示方式。

JSON is a structure that contains arrays, dictionaries ("objects" in javascript), strings, numbers, and null (NSNUll). JSON是一种结构,其中包含数组,字典(JavaScript中的“对象”),字符串,数字和null(NSNUll)。 You are trying to encode NSPoint s into JSON and JSON is not designed for that. 您正在尝试将NSPoint编码为JSON,而JSON并非为此设计的。 I suggest either converting the points to strings, or to objects (something like: {x:160, y:110}). 我建议将这些点转换为字符串或对象(类似:{x:160,y:110})。

see also https://developer.apple.com/reference/foundation/jsonserialization for a full description of what objects can be converted to JSON. 另请参阅https://developer.apple.com/reference/foundation/jsonserialization ,以获取有关可以将哪些对象转换为JSON的完整说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM