簡體   English   中英

無法讀取數據,因為格式不正確

[英]The data couldn’t be read because it isn’t in the correct format

好的,我現在開始使用iOS開發,目前使用Obj-C播放NSData。 最近我使用URLSession:dataTask:didReceiveData方法使用HTTP POST請求獲取NSData。 服務器將響應包含JSON數組的JSON對象。

有趣的是,當響應數據太大時,NSLog部分將打印出:“數據無法讀取,因為它的格式不正確” 以下是功能:

-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
    NSError *error;
    // get data from NSData into NSDict
    NSDictionary *searchData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    NSArray *test = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    NSMutableDictionary *mdict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    NSLog(@"what: %@", mdict);
    NSLog(@"received data length: %@", [NSByteCountFormatter stringFromByteCount:data.length countStyle:NSByteCountFormatterCountStyleFile]);
    if (error) {
        NSLog(@"json error: %@", [error localizedDescription]);
    }
}

只是想知道是否有人知道造成這個問題的潛在原因??

[更新]

好吧,解決此問題的另一種方法可能是使用NSString進行數據存儲。 但是我必須自己解析它。 我更喜歡使用NSDictionary。

初學者問題:在數據完成之前,當某些數據到達時調用didReceiveData。 閱讀didReceiveData的文檔,它解釋了您需要做什么。 您正在嘗試解析不完整的數據。 那不行。

當你正在處理JSON數據,你應該永遠不會涉及任何NSString對象,可能除了記錄數據。

在我的情況下,我正在使用POST方法而不是GET。 解決了我的問題。

首先,您應該檢查從API收到的響應是否是有效的JSON ,可以通過在線驗證器(如jsonlint.com)進行檢查。 如果這是一個vaid json,那么你很高興。

當你得到它時,你應該評估每個步驟的error對象,看看哪個數據解析會導致這個問題:

    NSError *error;
    // get data from NSData into NSDict
    NSDictionary *searchData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

    if (error) {
        NSLog(@"json error: %@", [error localizedDescription]);
    }

    NSArray *test = [NSKeyedUnarchiver unarchiveObjectWithData:data];


    NSMutableDictionary *mdict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    NSLog(@"what: %@", mdict);

    NSLog(@"received data length: %@", [NSByteCountFormatter stringFromByteCount:data.length countStyle:NSByteCountFormatterCountStyleFile]);

    if (error) {
        NSLog(@"json error: %@", [error localizedDescription]);
    }

另外,正如你所說:

服務器將響應包含JSON數組的JSON對象。

你確定它正在返回NS(Mutable)Dictionary嗎? 嘗試打印它像這樣:

    id response = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

    if (error) {
        NSLog(@"json error: %@", [error localizedDescription]);
    }
    else
    {
        NSLog(@"json data : %@", response);
    }

如果數據有點josn,你可以

NSDictionary *dict= [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];

要么

[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]) 

,這樣你就可以獲得nsdictionary或nsstring數據,如果你不能自己解析它,你可以使用jsonModel tranlte來建模。

這可能是我經歷過相同的json數據的問題。 您必須序列化您的json數據,如下面編寫的代碼

NSDictionary *dict= [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];

如果要從數據庫填充json數據,則必須使用Nsarray。 希望這會幫助你 。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM