Obj-C:将 NSDictionary 转换为 JSON 时应用程序崩溃

Posted

技术标签:

【中文标题】Obj-C:将 NSDictionary 转换为 JSON 时应用程序崩溃【英文标题】:Obj-C: Application crashing while converting NSDictionary to JSON 【发布时间】:2016-04-12 09:17:23 【问题描述】:

我正在阅读safari's Bookmarks.plist 并将其存储在NSDictionary 中,然后尝试将NSDictionary 转换为JSON

问题

书签数据正在进入字典,但是当我将数据转换为 JSON 时,应用程序崩溃了。

    NSString* path = @"path of the plist file";

    NSDictionary *plistDict = [NSDictionary dictionaryWithContentsOfFile:path];

    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:plistDict 
options:NSJSONWritingPrettyPrinted error:&error];


    NSString* result = [[NSString alloc] initWithData:jsonData 
encoding:NSUTF8StringEncoding];

原因

由 Plist 文件构成的 NSDictionary 包含一些属于 NSData 类型的字段。有没有办法将所有 NSData 字段转换为 Base64 字符串以清理 NSDictionary?请注意,不可能事先知道哪些字段和多少个字段是 NSData 类型。

错误日志

    2016-04-12 14:44:13.560 plistTOjosn[4228:68289] An uncaught exception was raised
    2016-04-12 14:44:13.560 plistTOjosn[4228:68289] Invalid type in JSON write (__NSCFData)
    2016-04-12 14:44:13.561 plistTOjosn[4228:68289] (
        0   CoreFoundation                      0x00007fff92d9b03c __exceptionPreprocess + 172
        1   libobjc.A.dylib                     0x00007fff914e176e objc_exception_throw + 43
        2   CoreFoundation                      0x00007fff92d9aeed +[NSException raise:format:] + 205
        3   Foundation                          0x00007fff91174b9a _writeJSONValue + 715
        4   Foundation                          0x00007fff91175cef ___writeJSONObject_block_invoke + 220
        5   CoreFoundation                      0x00007fff92d9a65c ____NSDictionaryEnumerate_block_invoke439 + 28
        6   CoreFoundation                      0x00007fff92c7b0b0 CFBasicHashApply + 128
        7   CoreFoundation                      0x00007fff92cbc118 __NSDictionaryEnumerate + 664
        8   Foundation                          0x00007fff91175b1f _writeJSONObject + 439
        9   Foundation                          0x00007fff91174ab6 _writeJSONValue + 487
        10  Foundation                          0x00007fff91175cef ___writeJSONObject_block_invoke + 220
        11  CoreFoundation                      0x00007fff92d9a65c ____NSDictionaryEnumerate_block_invoke439 + 28
        12  CoreFoundation                      0x00007fff92c7b0b0 CFBasicHashApply + 128
        13  CoreFoundation                      0x00007fff92cbc118 __NSDictionaryEnumerate + 664
        14  Foundation                          0x00007fff91175b1f _writeJSONObject + 439
        15  Foundation                          0x00007fff91174ab6 _writeJSONValue + 487
        16  Foundation                          0x00007fff9117489a -[_NSJSONWriter dataWithRootObject:options:error:] + 137
        17  Foundation                          0x00007fff91174765 +[NSJSONSerialization dataWithJSONObject:options:error:] + 345
        18  Utilities                           0x00000001000c7d0b -[BrowserJunkUtilities readPlistFileFromSafariProfiles:] + 331
        19  Utilities                           0x00000001000c7e4b -[BrowserJunkUtilities safariBookmarksJson] + 59
        20  Utilities                           0x00000001000bbd5c -[BrowserJunkUtilities LogInBigData] + 252
        21  Utilities                           0x00000001000c1af2 -[BrowserJunkUtilities init] + 5970
        22  plistTOjosn                            0x00000001000299e5 -[DashBoardView init] + 261
        23  plistTOjosn                            0x0000000100016e45 -[AppController changeViewController:RunFix:] + 757
        24  plistTOjosn                            0x0000000100018fe5 -[AppController showIntroView:tag:] + 181
        25  plistTOjosn                            0x0000000100016a7f -[AppController awakeFromNib] + 1103
        26  CoreFoundation                      0x00007fff92ca7bdf -[NSSet makeObjectsPerformSelector:] + 223
        27  AppKit                              0x00007fff9329e03d -[NSIBObjectData nibInstantiateWithOwner:options:topLevelObjects:] + 1216
        28  AppKit                              0x00007fff9327d0e5 loadNib + 384
        29  AppKit                              0x00007fff9327c60b +[NSBundle(NSNibLoading) _loadNibFile:nameTable:options:withZone:ownerBundle:] + 313
        30  AppKit                              0x00007fff9327c3c7 -[NSBundle(NSNibLoading) loadNibNamed:owner:topLevelObjects:] + 201
        31  AppKit                              0x00007fff9327c193 +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 344
        32  AppKit                              0x00007fff93274d79 NSApplicationMain + 605
        33  plistTOjosn                            0x0000000100007542 main + 34
        34  plistTOjosn                            0x00000001000016d4 start + 52
        35  ???                                 0x0000000000000003 0x0 + 3
    )
    2016-04-12 14:44:13.690 plistTOjosn[4228:68289] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__NSCFData)'
    *** First throw call stack:
    (
        0   CoreFoundation                      0x00007fff92d9b03c __exceptionPreprocess + 172
        1   libobjc.A.dylib                     0x00007fff914e176e objc_exception_throw + 43
        2   CoreFoundation                      0x00007fff92d9aeed +[NSException raise:format:] + 205
        3   Foundation                          0x00007fff91174b9a _writeJSONValue + 715
        4   Foundation                          0x00007fff91175cef ___writeJSONObject_block_invoke + 220
        5   CoreFoundation                      0x00007fff92d9a65c ____NSDictionaryEnumerate_block_invoke439 + 28
        6   CoreFoundation                      0x00007fff92c7b0b0 CFBasicHashApply + 128
        7   CoreFoundation                      0x00007fff92cbc118 __NSDictionaryEnumerate + 664
        8   Foundation                          0x00007fff91175b1f _writeJSONObject + 439
        9   Foundation                          0x00007fff91174ab6 _writeJSONValue + 487
        10  Foundation                          0x00007fff91175cef ___writeJSONObject_block_invoke + 220
        11  CoreFoundation                      0x00007fff92d9a65c ____NSDictionaryEnumerate_block_invoke439 + 28
        12  CoreFoundation                      0x00007fff92c7b0b0 CFBasicHashApply + 128
        13  CoreFoundation                      0x00007fff92cbc118 __NSDictionaryEnumerate + 664
        14  Foundation                          0x00007fff91175b1f _writeJSONObject + 439
        15  Foundation                          0x00007fff91174ab6 _writeJSONValue + 487
        16  Foundation                          0x00007fff9117489a -[_NSJSONWriter dataWithRootObject:options:error:] + 137
        17  Foundation                          0x00007fff91174765 +[NSJSONSerialization dataWithJSONObject:options:error:] + 345
        18  Utilities                           0x00000001000c7d0b -[BrowserJunkUtilities readPlistFileFromSafariProfiles:] + 331
        19  Utilities                           0x00000001000c7e4b -[BrowserJunkUtilities safariBookmarksJson] + 59
        20  Utilities                           0x00000001000bbd5c -[BrowserJunkUtilities LogInBigData] + 252
        21  Utilities                           0x00000001000c1af2 -[BrowserJunkUtilities init] + 5970
        22  plistTOjosn                            0x00000001000299e5 -[DashBoardView init] + 261
        23  plistTOjosn                            0x0000000100016e45 -[AppController changeViewController:RunFix:] + 757
        24  plistTOjosn                            0x0000000100018fe5 -[AppController showIntroView:tag:] + 181
        25  plistTOjosn                            0x0000000100016a7f -[AppController awakeFromNib] + 1103
        26  CoreFoundation                      0x00007fff92ca7bdf -[NSSet makeObjectsPerformSelector:] + 223
        27  AppKit                              0x00007fff9329e03d -[NSIBObjectData nibInstantiateWithOwner:options:topLevelObjects:] + 1216
        28  AppKit                              0x00007fff9327d0e5 loadNib + 384
        29  AppKit                              0x00007fff9327c60b +[NSBundle(NSNibLoading) _loadNibFile:nameTable:options:withZone:ownerBundle:] + 313
        30  AppKit                              0x00007fff9327c3c7 -[NSBundle(NSNibLoadin

g) loadNibNamed:owner:topLevelObjects:] + 201
    31  AppKit                              0x00007fff9327c193 +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 344
    32  AppKit                              0x00007fff93274d79 NSApplicationMain + 605
    33  plistTOjosn                            0x0000000100007542 main + 34
    34  plistTOjosn                            0x00000001000016d4 start + 52
    35  ???                                 0x0000000000000003 0x0 + 3
)
libc++abi.dylib: terminating with uncaught exception of type NSException

PLIST 数据


    Children =     (
                
            Title = History;
            WebBookmarkIdentifier = History;
            WebBookmarkType = WebBookmarkTypeProxy;
            WebBookmarkUUID = "019DBF83-1882-46AA-A8A7-669CCD3D5AB8";
        ,
                
            Children =             (
                                
                    Sync =                     
                        Key = "\"C=32@U=19016305-56c9-437c-95f4-93521bad2be8\"";
                        ServerID = "https://bnslvks%40icloud.com@p35-bookmarks.icloud.com/8402630368/bookmarks/2A662FEA-35F9-44C5-87B9-8DDEBE0E4912/63582BD8-0400-40C4-9CA7-8942391957B8.xbel";
                    ;
                    URIDictionary =                     
                        title = "vikas Technologies Pvt. Ltd Mail";
                    ;
                    URLString = "https://mail.google.com/mail/u/0/";
                    WebBookmarkType = WebBookmarkTypeLeaf;
                    WebBookmarkUUID = "AFDCB7C0-10CE-4273-BDF8-E2FC40D83A3A";
                ,
                                
                    Sync =                     
                        Key = "\"C=6@U=19016305-56c9-437c-95f4-93521bad2be8\"";
                        ServerID = "https://bnslvks%40icloud.com@p35-bookmarks.icloud.com/8402630368/bookmarks/2A662FEA-35F9-44C5-87B9-8DDEBE0E4912/474F816C-BB3E-4F42-89B4-666F5E323840.xbel";
                    ;
                    URIDictionary =                     
                        title = Apple;
                    ;
                    URLString = "https://www.apple.com/";
                    WebBookmarkType = WebBookmarkTypeLeaf;
                    WebBookmarkUUID = "4468D8F5-6A99-4B0A-85AD-E1E49B58546D";
                ,
                                
                    Sync =                     
                        Key = "\"C=8@U=19016305-56c9-437c-95f4-93521bad2be8\"";
                        ServerID = "https://bnslvks%40icloud.com@p35-bookmarks.icloud.com/8402630368/bookmarks/2A662FEA-35F9-44C5-87B9-8DDEBE0E4912/89401E9C-92BB-4935-91D1-F968FD90239B.xbel";
                    ;
                    URIDictionary =                     
                        title = iCloud;
                    ;
                    URLString = "https://www.icloud.com/";
                    WebBookmarkType = WebBookmarkTypeLeaf;
                    WebBookmarkUUID = "2E4E8D77-D2C4-4B79-A563-5FF5FED0D553";
                ,
                                
                    Sync =                     
                        Key = "\"C=10@U=19016305-56c9-437c-95f4-93521bad2be8\"";
                        ServerID = "https://bnslvks%40icloud.com@p35-bookmarks.icloud.com/8402630368/bookmarks/2A662FEA-35F9-44C5-87B9-8DDEBE0E4912/6B14FBB9-DA11-4F5B-BF85-4E997D73C720.xbel";
                    ;
                    URIDictionary =                     
                        title = Yahoo;
                    ;
                    URLString = "https://www.yahoo.com/";
                    WebBookmarkType = WebBookmarkTypeLeaf;
                    WebBookmarkUUID = "9CFD581F-2404-4BDF-8795-5D4DCC9A7CF0";
                ,
                                
                    Sync =                     
                        Key = "\"C=12@U=19016305-56c9-437c-95f4-93521bad2be8\"";
                        ServerID = "https://bnslvks%40icloud.com@p35-bookmarks.icloud.com/8402630368/bookmarks/2A662FEA-35F9-44C5-87B9-8DDEBE0E4912/0F0130B7-2F0C-4BD5-9173-E91C210F845C.xbel";
                    ;
                    URIDictionary =                     
                        title = Bing;
                    ;
                    URLString = "https://www.bing.com/";
                    WebBookmarkType = WebBookmarkTypeLeaf;
                    WebBookmarkUUID = "89FEEC32-AC16-4A80-8875-68BE4A191862";
                ,
                                
                    Sync =                     
                        Key = "\"C=14@U=19016305-56c9-437c-95f4-93521bad2be8\"";
                        ServerID = "https://bnslvks%40icloud.com@p35-bookmarks.icloud.com/8402630368/bookmarks/2A662FEA-35F9-44C5-87B9-8DDEBE0E4912/9554C3E5-61CD-40BE-BFB2-7FBEB22807C0.xbel";
                    ;
                    URIDictionary =                     
                        title = Google;
                    ;
                    URLString = "https://www.google.com/?client=safari&channel=mac_bm";
                    WebBookmarkType = WebBookmarkTypeLeaf;
                    WebBookmarkUUID = "5A013EF2-E017-41B3-8FAE-2DA718D35AC5";
                ,
                                
                    Sync =                     
                        Key = "\"C=16@U=19016305-56c9-437c-95f4-93521bad2be8\"";
                        ServerID = "https://bnslvks%40icloud.com@p35-bookmarks.icloud.com/8402630368/bookmarks/2A662FEA-35F9-44C5-87B9-8DDEBE0E4912/56EA5315-E1B1-430B-A018-7326A247ECAA.xbel";
                    ;
                    URIDictionary =                     
                        title = Wikipedia;
                    ;
                    URLString = "https://www.wikipedia.org/";
                    WebBookmarkType = WebBookmarkTypeLeaf;
                    WebBookmarkUUID = "49CE5DC7-6ABD-4876-95E4-0C7A007DED0D";
                ,
                                
                    Sync =                     
                        Key = "\"C=18@U=19016305-56c9-437c-95f4-93521bad2be8\"";
                        ServerID = "https://bnslvks%40icloud.com@p35-bookmarks.icloud.com/8402630368/bookmarks/2A662FEA-35F9-44C5-87B9-8DDEBE0E4912/FDF4AD0D-FD33-4EE2-9097-19CA5046AFB7.xbel";
                    ;
                    URIDictionary =                     
                        title = Facebook;
                    ;
                    URLString = "https://www.facebook.com/";
                    WebBookmarkType = WebBookmarkTypeLeaf;
                    WebBookmarkUUID = "530A47C6-D116-4081-9BED-3C469C7C7697";
                ,
                                
                    Sync =                     
                        Key = "\"C=20@U=19016305-56c9-437c-95f4-93521bad2be8\"";
                        ServerID = "https://bnslvks%40icloud.com@p35-bookmarks.icloud.com/8402630368/bookmarks/2A662FEA-35F9-44C5-87B9-8DDEBE0E4912/3E61C9CF-7646-4475-AC89-FB157DA50181.xbel";
                    ;
                    URIDictionary =                     
                        title = Twitter;
                    ;
                    URLString = "https://twitter.com/";
                    WebBookmarkType = WebBookmarkTypeLeaf;
                    WebBookmarkUUID = "D3893EF3-A885-41F6-A128-BBAC37A2033B";
                ,
                                
                    Sync =                     
                        Key = "\"C=22@U=19016305-56c9-437c-95f4-93521bad2be8\"";
                        ServerID = "https://bnslvks%40icloud.com@p35-bookmarks.icloud.com/8402630368/bookmarks/2A662FEA-35F9-44C5-87B9-8DDEBE0E4912/C8801D19-7A76-4290-8950-5DA9385F3415.xbel";
                    ;
                    URIDictionary =                     
                        title = LinkedIn;
                    ;
                    URLString = "https://www.linkedin.com/";
                    WebBookmarkType = WebBookmarkTypeLeaf;
                    WebBookmarkUUID = "47F6045D-4F9A-4856-916A-07CB8889B7E2";
                ,
                                
                    Sync =                     
                        Key = "\"C=24@U=19016305-56c9-437c-95f4-93521bad2be8\"";
                        ServerID = "https://bnslvks%40icloud.com@p35-bookmarks.icloud.com/8402630368/bookmarks/2A662FEA-35F9-44C5-87B9-8DDEBE0E4912/DF10E9C3-01FA-4EC6-ADE2-4A0B1FECD9EF.xbel";
                    ;
                    URIDictionary =                     
                        title = "The Weather Channel";
                    ;
                    URLString = "http://www.weather.com/";
                    WebBookmarkType = WebBookmarkTypeLeaf;
                    WebBookmarkUUID = "0DDD3778-437A-4FAE-8529-D389A5122758";
                ,
                                
                    Sync =                     
                        Key = "\"C=26@U=19016305-56c9-437c-95f4-93521bad2be8\"";
                        ServerID = "https://bnslvks%40icloud.com@p35-bookmarks.icloud.com/8402630368/bookmarks/2A662FEA-35F9-44C5-87B9-8DDEBE0E4912/79C2C180-430C-4092-A4CF-D2B8F4551B1E.xbel";
                    ;
                    URIDictionary =                     
                        title = Yelp;
                    ;
                    URLString = "http://www.yelp.com/";
                    WebBookmarkType = WebBookmarkTypeLeaf;
                    WebBookmarkUUID = "2FD1AB60-B3A0-44D5-A20D-68CF51A40584";
                ,
                                
                    Sync =                     
                        Key = "\"C=28@U=19016305-56c9-437c-95f4-93521bad2be8\"";
                        ServerID = "https://bnslvks%40icloud.com@p35-bookmarks.icloud.com/8402630368/bookmarks/2A662FEA-35F9-44C5-87B9-8DDEBE0E4912/63EDC45D-3138-41CF-A617-4178097656F0.xbel";
                    ;
                    URIDictionary =                     
                        title = TripAdvisor;
                    ;
                    URLString = "http://www.tripadvisor.com/";
                    WebBookmarkType = WebBookmarkTypeLeaf;
                    WebBookmarkUUID = "A6DA8288-409E-4B03-84E4-ACEF9F2B624C";
                ,
                                
                    Sync =                     
                        Key = "\"C=30@U=19016305-56c9-437c-95f4-93521bad2be8\"";
                        ServerID = "https://bnslvks%40icloud.com@p35-bookmarks.icloud.com/8402630368/bookmarks/2A662FEA-35F9-44C5-87B9-8DDEBE0E4912/40F8D2AF-4F2E-44BE-8250-41E1C7C0F1D9.xbel";
                    ;
                    URIDictionary =                     
                        title = "ADplus.aspx";
                    ;
                    URLString = "http://dev.etelmar.net/ADEV_ADplus3-01/ADplus.aspx";
                    WebBookmarkType = WebBookmarkTypeLeaf;
                    WebBookmarkUUID = "C5D3ADD4-DB17-421D-BAF5-04927F713BCE";
                
            );
            Sync =             
                ServerID = "https://bnslvks%40icloud.com@p35-bookmarks.icloud.com/8402630368/bookmarks/2A662FEA-35F9-44C5-87B9-8DDEBE0E4912/";
            ;
            Title = BookmarksBar;
            WebBookmarkType = WebBookmarkTypeList;
            WebBookmarkUUID = "E2EC6281-F0F9-41FA-9C2C-2DC8745F547A";
        ,
                
            Sync =             
                ServerID = "https://bnslvks%40icloud.com@p35-bookmarks.icloud.com/8402630368/bookmarks/C81C19F2-E6A6-46C7-9836-DC58A02B46AB/";
            ;
            Title = BookmarksMenu;
            WebBookmarkType = WebBookmarkTypeList;
            WebBookmarkUUID = "06621BC7-DAA3-4225-8BCB-A65A8E0D0916";
        
    );
    Sync =     
        ServerData = <62706c69 73743030 de010203 04050607 08090a0b 0c0d0e0f 10111213 14151624 25303120 20545054 61675443 54616757 50757368 4b65795d 426f6f6b 6d61726b 42617249 645d486f 6d655552 4c537472 696e675e 426f6f6b 6d61726b 4d656e75 49645c41 63636f75 6e745072 7349645c 42756c6b 52657175 65737473 5953796e 63546f6b 656e5e50 75736854 72616e73 706f7274 735d436c 69656e74 56657273 696f6e5f 10125072 696e6369 70616c55 524c5374 72696e67 5f101653 7570706f 72747353 796e6343 6f6c6c65 6374696f 6e5f100f 496e6974 69616c53 796e6344 6f6e655f 10195365 72766572 446f6573 4e6f7453 7570706f 72745054 6167735f 10314654 3d2d4052 553d3139 30313633 30352d35 3663392d 34333763 2d393566 342d3933 35323162 61643262 65384053 3d33315a 38343032 36333033 36385f10 72687474 70733a2f 2f62616e 73616c76 6b732534 3069636c 6f75642e 636f6d40 7033352d 626f6f6b 6d61726b 732e6963 6c6f7564 2e636f6d 2f383430 32363330 3336382f 626f6f6b 6d61726b 732f3241 36363246 45412d33 3546392d 34344335 2d383742 392d3844 44454245 30453439 31322f5f 104d6874 7470733a 2f2f6261 6e73616c 766b7325 34306963 6c6f7564 2e636f6d 40703335 2d626f6f 6b6d6172 6b732e69 636c6f75 642e636f 6d2f3834 30323633 30333638 2f626f6f 6b6d6172 6b732f5f 10726874 7470733a 2f2f6261 6e73616c 766b7325 34306963 6c6f7564 2e636f6d 40703335 2d626f6f 6b6d6172 6b732e69 636c6f75 642e636f 6d2f3834 30323633 30333638 2f626f6f 6b6d6172 6b732f43 38314331 3946322d 45364136 2d343643 372d3938 33362d44 43353841 30324234 3641422f 5a383430 32363330 333638d2 17181922 54637275 64567369 6d706c65 d41a1b1c 1d1e1f20 20586d61 782d7369 7a655d6d 61782d72 65736f75 72636573 56757064 61746556 696e7365 72741200 a0000010 c80909d3 1b1d1a1f 201e095f 103e4441 5653542d 56312d70 33352d46 543d2d40 52553d31 39303136 3330352d 35366339 2d343337 632d3935 66342d39 33353231 62616432 62653840 533d3333 d1262754 41505344 d428292a 2b2c2d2e 2f5b6170 7362756e 646c6569 6453656e 765f1010 73756273 63726970 74696f6e 2d75726c 5f101072 65667265 73682d69 6e746572 76616c5f 1010636f 6d2e6d65 2e626f6f 6b6d6172 6b735a50 524f4455 4354494f 4e5f1053 68747470 733a2f2f 62616e73 616c766b 73253430 69636c6f 75642e63 6f6d4070 33352d62 6f6f6b6d 61726b73 2e69636c 6f75642e 636f6d2f 38343032 36333033 36382f6d 6d2f7075 73682f72 65676973 74657256 31323030 30301001 5f104d68 74747073 3a2f2f62 616e7361 6c766b73 25343069 636c6f75 642e636f 6d407033 352d626f 6f6b6d61 726b732e 69636c6f 75642e63 6f6d2f38 34303236 33303336 382f7072 696e6369 70616c2f 09090008 0025002a 002f0037 00450053 0062006f 007c0086 009500a3 00b800d1 00e300ff 0133013e 01b30203 02780283 0288028d 0294029d 02a602b4 02bb02c2 02c702c9 02ca02cb 02d202d3 03140317 031c0325 03310335 0348035b 036e0379 03cf03d6 03d80428 04290000 00000000 02010000 00000000 00340000 00000000 00000000 00000000 042a>;
    ;
    Title = "";
    WebBookmarkFileVersion = 1;
    WebBookmarkType = WebBookmarkTypeList;
    WebBookmarkUUID = "FBAA875A-C10A-4744-98BD-DC5EC1D9A009"; 

【问题讨论】:

你想从这些数据中获取什么? 一切。我的意思是整个文件@AayushKatiyar。 你可以看这里这也是类似的问题***.com/questions/9893217/… 【参考方案1】:

在将任何对象转换为 json 之前,首先检查给定对象是否可转换为 json,您可以使用以下代码进行检查

if ([NSJSONSerialization isValidJSONObject: plistDict])

    ....

因为在传递 NSString 时,JSON 对象必须是 NSArray 或 NSDictionary 类型。

来自docs:

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

***对象是 NSArray 或 NSDictionary。 所有对象都是 NSString、NSNumber、NSArray、NSDictionary 或 NSNull 的实例。 所有字典键都是 NSString 的实例。 数字不是 NaN 或无穷大。

【讨论】:

它总是NO,因为它里面有NSData类型的变量。是否可以从 dic 中删除所有 NSData 对象?? 是的,那么不可能把那个对象转成json 如何从 NSDictionary 中删除所有 NSdata 对象?不知道有多少变量将是 NSData 的类型?非常感谢 你应该把那个NSData去掉或者转换成NSString再转换……否则不能直接转换 我知道这就是问题所在。它没有定义有多少和哪些变量将是 NSData 类型,因为safari 正在创建 plist 文件。【参考方案2】:

您的数据结构可以成功读入 NSDictionary 实例。但是,当您将其序列化为 JSON 时,它会失败,因为它包含无法表示为 JSON 的部分。

我的猜测是$.Sync.ServerData 的数据。这似乎是二进制数据。 JSON 没有二进制数据的数据类型。

在序列化之前删除此元素。或者用其他东西替换它,例如具有二进制数据的 Base-64 表示的字符串。

【讨论】:

有没有办法删除所有NSData类型的值和键?【参考方案3】:

关键信息是

JSON 写入中的类型无效 (__NSCFData)

JSON 不支持NSData 类型(ServerData 的值),您可以将数据作为 Base64 编码的字符串发送。

【讨论】:

【参考方案4】:

这是关键:

Invalid type in JSON write (__NSCFData)

您不能将NSData 序列化为 JSON。尝试使用 base-64 将其编码为字符串(请参阅this answer 来做到这一点)。

【讨论】:

是否有跳过__NSCFData 和 JSONFY 其余部分的解决方法 复制字典并删除密钥。【参考方案5】:

这样做可能对你有帮助

   NSURLResponse *response;
        NSError *err;
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];


        NSString *str=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];

        NSDictionary *dict6 = [self cleanJsonToObject:responseData];

        self.handmadeEmbDict = [dict6 objectForKey:@"hemb_list"];
        [handmadeEmbTable reloadData];



    
- (id)cleanJsonToObject:(id)data 
    NSError* error;
    if (data == (id)[NSNull null])
        return [[NSObject alloc] init];
    
    id jsonObject;
    if ([data isKindOfClass:[NSData class]])
        jsonObject = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
     else 
        jsonObject = data;
    
    if ([jsonObject isKindOfClass:[NSArray class]]) 
        NSMutableArray *array = [jsonObject mutableCopy];
        for (int i = (int)array.count-1; i >= 0; i--) 
            id a = array[i];
            if (a == (id)[NSNull null])
                [array removeObjectAtIndex:i];
             else 
                array[i] = [self cleanJsonToObject:a];
            
        
        return array;
     else if ([jsonObject isKindOfClass:[NSDictionary class]]) 
        NSMutableDictionary *dictionary = [jsonObject mutableCopy];
        for(NSString *key in [dictionary allKeys]) 
            id d = dictionary[key];
            if (d == (id)[NSNull null])
                dictionary[key] = @"";
             else 
                dictionary[key] = [self cleanJsonToObject:d];
            
        
        return dictionary;
     else 
        return jsonObject;
    

【讨论】:

你把问题反过来了。问题不在于将 json 转换为 dict。相反,问题在于从 dict 创建 json 时。由于字典中存在 NSData 类型的数据。在随机的地方。【参考方案6】:

斯威夫特 2

 if let path = NSBundle.mainBundle().pathForResource("try", ofType: "json")

    do 
    let jsonData  = try NSData(contentsOfFile:
    path, options: NSDataReadingOptions.MappedRead)
    let dict = try   NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableLeaves)
    let rootDict = dict
    Print(rootDict)
    catch
    

    

【讨论】:

以上是关于Obj-C:将 NSDictionary 转换为 JSON 时应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

从具有属性的 NSObject 转换为 NSDictionary 的 Obj-C 简单方法?

如何将 obj-c NSDictionary 传递给 dart

将 NSDictionary 与内部函数解码为 swift 对象的值

Obj-C,iOS,如何将基于导航的应用程序转换为基于视图的应用程序?

如何修复错误无法将 [NSDictionary] 快速转换为 NSDictionary?

将 NSDictionary 转换为 Swift 字典