久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

NSJSONSerialization 出錯 - JSON 寫入中的類型無效(菜單

Error with NSJSONSerialization - Invalid type in JSON write (Menu)(NSJSONSerialization 出錯 - JSON 寫入中的類型無效(菜單))
本文介紹了NSJSONSerialization 出錯 - JSON 寫入中的類型無效(菜單)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我有一個使用核心數據的應用程序,其中包含 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

每個實體都有一個關聯的類:示例

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]);

但是我得到一個 SIGABRT 錯誤,而不是 JSON.

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?

推薦答案

那是因為你的菜單"類在 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 類參考

可以轉換為 JSON 的對象必須具有以下內容屬性:

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

  • 頂級對象是 NSArray 或 NSDictionary.
  • 所有對象都是 NSString、NSNumber、NSArray、NSDictionary 或 NSNull 的實例.
  • 所有字典鍵都是 NSString 的實例.
  • 數字不是 NaN 或無窮大.

這意味著該語言知道如何序列化字典.因此,從菜單中獲取 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 出錯 - JSON 寫入中的類型無效(菜單)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

KIF: How to auto-run/stress test an iOS app to find the cause of a rare UI bug?(KIF:如何自動運行/壓力測試 iOS 應用程序以找出罕見 UI 錯誤的原因?)
UITableView: Handle cell selection in a mixed cell table view static and dynamic cells(UITableView:在混合單元格表視圖靜態和動態單元格中處理單元格選擇)
How to remove Address Bar in Safari in iOS?(如何在 iOS 中刪除 Safari 中的地址欄?)
Having trouble creating UIImage from CIImage in iOS5(在 iOS5 中從 CIImage 創建 UIImage 時遇到問題)
Get list of all photo albums and thumbnails for each album(獲取所有相冊的列表和每個相冊的縮略圖)
Can anybody help me in recording iPhone output sound through Audio Unit(誰能幫我通過音頻單元錄制 iPhone 輸出的聲音)
主站蜘蛛池模板: 8x8ⅹ国产精品一区二区 | 91av视频在线播放 | 欧美成人精品欧美一级乱黄 | 欧美黄色一级视频 | 超碰在线观看免费 | 国产成人a亚洲精品 | 黄色片中文字幕 | 午夜高清 | 午夜视频一区二区 | 亚洲一区二区中文字幕 | 黄色成年人视频 | 99精品久久久久久 | 国产一区二区在线播放 | 欧美激情一二三区 | 福利小视频 | 欧美在线观看视频 | 超碰在线人人 | 国产一区二区不卡视频 | 久热精品视频 | 亚洲成人免费av | 激情久久五月天 | 欧美激情三区 | 亚洲高清免费 | 国产黄色片视频 | 高清免费av| 欧美黄网站 | 天天干天天操天天干 | 日本中文字幕在线播放 | aaa免费视频 | 欧美日韩国产精品 | 97成人免费视频 | 视频一区在线播放 | 精品一二区 | 成人看片免费 | 日韩中文字幕视频 | 成人看片网 | 国产精品久久久一区二区 | 亚洲区视频 | 亚洲免费网站 | 欧美大片一区二区 | 国产精品麻豆免费版 |