我想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;
}
)
自定義對象是不有效的JSON數據。閱讀「NSJSONSerialization」的文檔以獲取有效的JSON數據的描述。 – rmaddy
這個問題很可能是字典中的NSDate值不符合JSON。 – vadian
是的,它是日期格式,一旦我得到了正確的排序,它可以工作 –