I have a method where I want to return either an NSData object or an 'NSString' that must be a JSON object in format.
At the moment this is what I have;
-(NSData *)JSONData{
NSMutableArray* arr = [NSMutableArray array];
for (int j = 0; j < self.sales.subArray.count; j++)
{
SalesObject* subCategory = [self.sales.subArray objectAtIndex:j];
NSDictionary * dict =[NSDictionary dictionaryWithObjectsAndKeys:
@"category_id",subCategory.category_id,
@"discounted",@"0",
@"price",subCategory.price,
@"active",subCategory.isActive, nil];
NSLog(@"Dict %@",dict);
[arr addObject:dict];
}
NSLog(@"Arr %@",arr);
NSLog(@"Arr %@",arr);
NSString *string = [arr description];
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:string options:kNilOptions error:nil];
NSLog(@"JSON Data %@",jsonData);
return jsonData;
}
As you can see I tried to convert an NSMutableArray to an NSData object but it didnt work. I get;
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid (non-string) key in JSON dictionary'
I now get the following error;
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write'
NSLog(@"Arr %@",arr);?