我正在尝试JSONSerialize我从NSFetchRequest获得的NSArray,但是当我调用isValidJSONObject时,我最终在它告诉我它无效的部分
代码如下,我最终在"不工作"参与调试。
+ (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;
}
)
答案 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。但是,根据您的代码和自定义我自己的数组返回有效。检查数组对象是否正确,还要检查该数组是否为空。如果array为空,则返回false。
答案 1 :(得分: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"];