Objective-C Json 使用

时间:2023-03-09 16:46:05
Objective-C Json 使用

Objective-c json

];

for(int i  = 0;i<myProduct.count;++i)
{
    //NSLog(@"----------------------");
    //NSLog(@"Product title: %@" ,[myProduct[i] localizedTitle]);
    //NSLog(@"Product description: %@" ,[myProduct[i] localizedDescription]);
    //NSLog(@"Product price: %@" ,[myProduct[i] price]);
    //NSLog(@"Product id: %@" ,[myProduct[i] productIdentifier]);
    myDict = [NSDictionary dictionaryWithObjectsAndKeys:
                    [myProduct[i] localizedTitle], @"title",
                    [myProduct[i] localizedDescription], @"desc",
                    [myProduct[i] price], @"price",
                    [myProduct[i] productIdentifier], @"product", nil];
    [dict setValue: myDict forKey: [myProduct[i] productIdentifier]];
}
if([NSJSONSerialization isValidJSONObject:dict])
{
    NSError* error;
    NSData *str = [NSJSONSerialization dataWithJSONObject:dict
                        options:kNilOptions error:&error];
    NSLog(@"Result: %@",[[NSString alloc]initWithData:str
                            encoding:NSUTF8StringEncoding]);
}
else
{
    NSLog(@"An error happened while serializing the JSON data.");
}