久久久久久久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 輸出的聲音)
主站蜘蛛池模板: 美女网站视频免费黄 | 欧美在线观看一区 | 激情小说综合网 | 国产黄色在线 | 91久久精品国产 | 久久久.com | 午夜精品久久久久久久99黑人 | 欧美亚州 | 国产高清一区二区 | 精品一区av| 在线国产一区 | 日本不卡一区 | 色综合色综合网色综合 | 黄色片免费| 91精品久久久久久久久中文字幕 | 中文字幕亚洲精品在线观看 | 亚洲在线 | 91麻豆精品国产91久久久更新资源速度超快 | 国产成人在线播放 | 国产日韩一区二区三免费高清 | 成人依人| 国产精品一区二区久久精品爱微奶 | 国产欧美精品一区二区三区 | 国产成人精品免高潮在线观看 | 国产精品美女在线观看 | 自拍偷拍小视频 | 日韩一三区 | 日韩久久中文字幕 | 欧美精品二区 | 日韩视频成人 | 日韩中文字幕一区二区三区 | 精品福利在线 | 国产高清精品网站 | 国产成人综合一区二区三区 | 国产激情91久久精品导航 | 成人1区2区 | 午夜国产一级 | 亚洲精品无 | 亚洲成人精选 | 精品国产18久久久久久二百 | 国产精品欧美一区二区 |