2016-12-24 179 views
0

我想JSONSerialize一個NSArray,我從一個NSFetchRequest得到,但是當我打電話isValidJSONObject我在節結束它告訴我,它是無效的爲什麼我不能JSONSerialize這個NSArray?

的代碼如下,我在結束了調試中「不起作用」的部分。

+ (int) synchOrder:(NSString *)orderNumber { 
    if ([NWTillHelper isDebug] == 1) { 
     NSLog(@"WebServices:synchOrder:ordernumber = %@", orderNumber); 
    } 

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate]; 

    NSManagedObjectContext *context = appDelegate.persistentContainer.viewContext; 

    // *** Fetch the orderHead information 

    // *** The query for all tables uses orderNumber as selection so we set that up first for re use 
    NSPredicate *predicateOrderNumber =[NSPredicate predicateWithFormat:@"orderNumber like[cd] %@", [NWTillHelper getCurrentOrderNumber]]; 

    NSFetchRequest *fetchRequestOh = [[NSFetchRequest alloc] initWithEntityName:@"OrderHead"]; 
    NSFetchRequest *fetchRequestOrp = [[NSFetchRequest alloc] initWithEntityName:@"OrderRow"]; 
    NSFetchRequest *fetchRequestTender = [[NSFetchRequest alloc] initWithEntityName:@"Tender"]; 

    fetchRequestOh.predicate = predicateOrderNumber; 
    fetchRequestOrp.predicate = predicateOrderNumber; 
    fetchRequestTender.predicate = predicateOrderNumber; 

    fetchRequestOh.resultType = NSDictionaryResultType; 
    fetchRequestOrp.resultType = NSDictionaryResultType; 
    fetchRequestTender.resultType = NSDictionaryResultType; 

    NSError *errorOh = nil; 
    NSArray *orderHeads = [[context executeFetchRequest:fetchRequestOh error:&errorOh] mutableCopy]; 

    NSError *errorOrp = nil; 
    NSArray *orderRows = [[context executeFetchRequest:fetchRequestOrp error:&errorOrp] mutableCopy]; 

    NSError *errorTender = nil; 
    NSArray *tenderRows = [[context executeFetchRequest:fetchRequestTender error:&errorTender] mutableCopy]; 

    if ([NWTillHelper isDebug] == 1) { 
     NSLog(@"WebServices:synchOrder:orderHeadsArray: %@", orderHeads); 
     NSLog(@"WebServices:synchOrder:orderRowsArray: %@", orderRows); 
     NSLog(@"WebServices:synchOrder:tenderRowsArray: %@", tenderRows); 
    } 

    // *** first lets upload orderHead 
    NSError *errorWebSvsOhApi; 

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 

    NSURLSession *sessionOrderHead = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil]; 

    NSURL *url = [NSURL URLWithString:@"https://XXX.YYY.ZZ:0000/path/to/api"]; 

    NSMutableURLRequest *requestOrderHeads = [NSMutableURLRequest requestWithURL:url 
                  cachePolicy:NSURLRequestUseProtocolCachePolicy 
                 timeoutInterval:60.0]; 

    if ([NSJSONSerialization isValidJSONObject:orderHeads]) { 
NSLog(@"works"); 
} else { 
NSLog(@"Does not work"); 
} 

該數組的調試輸出如下所示。

2016-12-24 11:55:38.362 NWMobileTill[1007:69570] WebServices:synchOrder:orderHeadsArray: (
     { 
     amountTax = 0; 
     companyId = Kalle; 
     createdDateUtc = "2016-12-24 03:55:25 +0000"; 
     isSynched = 0; 
     orderNumber = "1-20161224115525"; 
     orderType = 0; 
     status = 0; 
     sumAfterTrDiscount = 0; 
     sumBeforeTrDiscount = 0; 
     tillId = 1; 
     trDiscountAmount = 0; 
     trDiscountPercentage = 0; 
     userName = 1; 
    } 
) 
+1

自定義對象是不有效的JSON數據。閱讀「NSJSONSerialization」的文檔以獲取有效的JSON數據的描述。 – rmaddy

+1

這個問題很可能是字典中的NSDate值不符合JSON。 – vadian

+0

是的,它是日期格式,一旦我得到了正確的排序,它可以工作 –

回答

0

它必須工作。這是來自您自己的數據和代碼的實驗。它的工作正常,如你所料。

NSArray *test = @[@{@"amountTax" : @0, 
        @"companyId" : @"Kalle", 
        @"createdDateUtc" : @"2016-12-24 03:55:25 +0000", 
        @"isSynched" : @(false), 
        @"orderNumber" : @"1-20161224115525", 
        @"orderType" : @0, 
        @"status" : @(false), 
        @"sumAfterTrDiscount" : @0, 
        @"sumBeforeTrDiscount" : @0, 
        @"tillId" : @1, 
        @"trDiscountAmount" : @0, 
        @"trDiscountPercentage" : @0, 
        @"userName" : @1 
        }]; 

NSLog(@"Test:%@", test); 

if ([NSJSONSerialization isValidJSONObject:test]) { 
    NSLog(@"works"); 
} else { 
    NSLog(@"Does not work"); 
} 

調試輸出:

Test:(
    { 
    amountTax = 0; 
    companyId = Kalle; 
    createdDateUtc = "2016-12-24 03:55:25 +0000"; 
    isSynched = 0; 
    orderNumber = "1-20161224115525"; 
    orderType = 0; 
    status = 0; 
    sumAfterTrDiscount = 0; 
    sumBeforeTrDiscount = 0; 
    tillId = 1; 
    trDiscountAmount = 0; 
    trDiscountPercentage = 0; 
    userName = 1; 
} 
) 
2016-12-24 13:38:34.098 sample[2628:58572] works 

isValidJSONObject用於返回一個布爾值,指示一個給定的對象是否可以被轉換爲JSON數據。如果您使用無效的NSArray對象進行檢查,它將返回false。但是,根據您的代碼和自定義我自己的數組返回有效。檢查數組對象是否正確,並檢查數組是否爲空。如果數組爲空,它將返回爲false。

+0

可能是因爲你把NSDate寫成了NSString! –

+0

是的,我寫成一個字符串。 –

+0

所以這是不一樣的,你沒有給出答案;) –

0

的問題是,NSDate的不能序列化爲Vadian在註釋中指出,通過使用以下日期格式它解決了這個問題,並在NSArray中可適當JSONSerialized

創建如下的日期和設置dateFormatter

NSDate *createdDate = [NSDate date]; 

NSDateFormatter *dateFormatterWithTz = [[NSDateFormatter alloc] init]; 
[dateFormatterWithTz setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"]; 

然後保存日期持久性存儲時所使用的格式如下

[newTenderRow setValue:[dateFormatterWithTz stringFromDate:createdDate] forKey:@"createdDate"]; 
相關問題