久久久久久久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 輸出的聲音)
主站蜘蛛池模板: 韩国电影久久 | 国产亚洲精品精品国产亚洲综合 | 天天操人人干 | 欧美一级久久久猛烈a大片 日韩av免费在线观看 | 国产一区二区三区四区hd | 精品99爱视频在线观看 | 巨大黑人极品videos精品 | 欧美一级在线 | 日日天天 | 99国产精品99久久久久久 | 成人av看片 | 在线成人 | 久久久久一区二区三区 | 小h片免费观看久久久久 | 欧美国产视频 | 欧美一区二区三区在线观看 | 天天插天天狠天天透 | 超碰97av | 亚洲精品在线免费看 | 亚洲区一区二 | 亚洲精品1| 日皮视频免费 | 久久久蜜桃一区二区人 | 国产精品久久久久久久午夜片 | 欧美日韩一区二区三区四区 | 色妹子综合网 | 涩涩视频在线观看 | 国产精品亚洲精品 | 日本黄视频在线观看 | 一级黄在线观看 | 亚洲国产高清免费 | 国产激情精品一区二区三区 | 性一交一乱一伦视频免费观看 | 久久伊人精品 | aaaa网站| 久久一级大片 | 久草视频观看 | 国产网站久久 | 欧美亚洲国语精品一区二区 | 国产欧美一区二区精品忘忧草 | 亚洲午夜精品 |