久久久久久久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 輸出的聲音)
主站蜘蛛池模板: 日韩欧美在线播放 | 欧美激情视频一区二区三区免费 | 国产精品久久一区 | 91av在线免费 | 日韩一二区| a级大片免费观看 | 99reav| 午夜精品久久久久久久 | 成人免费av | 影音先锋中文字幕在线观看 | 99热激情 | www精品美女久久久tv | 久www| 免费视频久久久久 | 日韩电影一区 | 81精品国产乱码久久久久久 | 成人亚洲网站 | 国产精品一区二区av | 亚洲成人在线网 | 在线高清免费观看视频 | 国产美女一区二区 | 国产亚洲人成a在线v网站 | 日本中文字幕一区 | 欧美日韩国产精品一区 | 在线日韩 | 成人国产精品色哟哟 | 久色网| 亚洲欧美综合精品久久成人 | 三级视频在线观看 | 国产成人福利在线观看 | 亚洲欧美日韩中文字幕一区二区三区 | 51ⅴ精品国产91久久久久久 | www久久国产 | 中文字幕在线观看 | 亚洲成人一区二区 | 久久日韩粉嫩一区二区三区 | 欧美日韩视频在线第一区 | 国产高清在线精品一区二区三区 | 丁香综合| 成人影院网站ww555久久精品 | 国产免费a |