1.字典转json
-(NSString*)dictionaryToJson:(NSDictionary *)dic { NSError *parseError = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError]; return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; }
2. 将NSArray 转换为JSON字符串传给后台
NSArray *trayCodes = @[@"123",@"456"]; NSError *error = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:trayCodes options:kNilOptions error:&error]; NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
3. 将NSArray 用分割符分割为字符串
NSArray *trayCodes = @[@"123",@"456"]; NSString *jsonStr = [trayCodes componentsJoinedByString:@","];