为什么我不能JSONSerialize这个NSArray?
我想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;
}
)
它必须工作。这是来自您自己的数据和代码的实验。它的工作正常,如你所料。
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。
可能是因为你把NSDate写成了NSString! –
是的,我写成一个字符串。 –
所以这是不一样的,你没有给出答案;) –
的问题是,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"];
自定义对象是不有效的JSON数据。阅读“NSJSONSerialization”的文档以获取有效的JSON数据的描述。 – rmaddy
这个问题很可能是字典中的NSDate值不符合JSON。 – vadian
是的,它是日期格式,一旦我得到了正确的排序,它可以工作 –