NSJSONSerialization出错

编程入门 行业动态 更新时间:2024-10-27 21:17:35
本文介绍了NSJSONSerialization出错 - JSON写入中的类型无效(菜单)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个应用程序使用核心数据与3个具有非常相似属性的实体。这种关系如下:

I have an App using core data with 3 entities with very similar attributes. The relationship is such as:

分支 - >>菜单 - >>类别 - >> FoodItem

Branch ->> Menu ->> Category ->> FoodItem

每个entity有一个关联的类:example

Each entity has an associated class: example

我正在尝试在sqlite数据库中生成数据的JSON表示。

//gets a single menu record which has some categories and each of these have some food items id obj = [NSArray arrayWithObject:[[DataStore singleton] getHomeMenu]]; NSError *err; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:obj options:NSJSONWritingPrettyPrinted error:&err]; NSLog(@"JSON = %@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);

但是我没有JSON,而是收到SIGABRT错误。

But instead of JSON, i get a SIGABRT error.

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

任何想法如何解决它或如何使实体类(分支,菜单等)JSON序列化兼容?

Any ideas how to fix it or how to make the entity classes (Branch, Menu etc) JSON serialization compatible?

推荐答案

这是因为您的Menu类在JSON中不可序列化。基本上,语言不知道你的对象应该如何用JSON表示(要包括哪些字段,如何表示对其他对象的引用......)

That's because your "Menu" class is not serializable in JSON. Bascially the language doesn't know how your object should be represented in JSON (which fields to include, how to represent references to other objects...)

来自 NSJSONSerialization Class Reference

可转换为JSON的对象必须具有以下属性:

An object that may be converted to JSON must have the following properties:

  • 顶级对象是NSArray或NSDictionary。
  • 所有对象都是NSString,NSNumber,NSArray,NSDictionary或NSNull的实例。
  • 所有字典键都是NSString的实例。
  • 数字不是NaN或无穷大。
  • The top level object is an NSArray or NSDictionary.
  • All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.
  • All dictionary keys are instances of NSString.
  • Numbers are not NaN or infinity.

这表示该语言知道如何序列化词典。因此,从菜单中获取JSON表示的一种简单方法是提供Menu实例的Dictionary表示,然后将序列化为JSON:

This means that the language knows how to serialize dictionaries. So a simple way to get a JSON representation from your menu is to provide a Dictionary representation of your Menu instances, which you will then serialize into JSON:

- (NSDictionary *)dictionaryFromMenu:(Menu)menu { [NSDictionary dictionaryWithObjectsAndKeys:[menu.dateUpdated description],@"dateUpdated", menu.categoryId, @"categoryId", //... add all the Menu properties you want to include here nil]; }

您可以像这样使用它:

NSDictionary *menuDictionary = [self dictionaryFromMenu:[[DataStore singleton] getHomeMenu]]; NSError *err; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:menuDictionary options:NSJSONWritingPrettyPrinted error:&err]; NSLog(@"JSON = %@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);

更多推荐

NSJSONSerialization出错

本文发布于:2023-11-26 04:47:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632684.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NSJSONSerialization

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!