Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid (non-string) key in JSON dictionary

the final line always throw a exception:"invalid key in JSON dictionary". I cant figure out why. I can print the "msg" dictionary.

NSDictionary* header = @{
@(udpVersion) : @"ver",
@(self.dataType) : @"type",
@(self.ack) : @"ack",
[WBUserMng sharedClient].getUserId : @"src",
};

NSDictionary* msg = @{
header:@"head",
self.payload:@"data",
};

NSError* error = nil;
return [NSJSONSerialization dataWithJSONObject:msg options:kNilOptions error:&error]
like image 512
sinopec Avatar asked Dec 08 '25 14:12

sinopec


1 Answers

I think you have the wrong order of key and value in the new Objective C syntax for dictionary literals. It should probably be

NSDictionary* msg = @{
    @"head" : header,
    @"data" : self.payload
};

and the same for the header dictionary.

like image 121
Martin R Avatar answered Dec 10 '25 04:12

Martin R