gpt4 book ai didi

ios - json数据到服务器

转载 作者:行者123 更新时间:2023-12-01 19:15:29 25 4
gpt4 key购买 nike

我正在开发一个应用程序,其中我必须以deviceToken格式将uuidJSON发送到服务器,例如:"regid":"x1y2z3","uuid":"1a2b3c"如何将NSString s以此格式存储到NSData中并将其发送到服务器?
regid字符串类似于x1y2z3uuid字符串类似于1a2b3c

我的代码:

 PushNotification* pushHandler = [self.viewController getCommandInstance:@"PushNotification"];
[pushHandler didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];

NSString *deviceT = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
NSString *tkStr = [[NSString alloc]initWithData:deviceToken encoding:NSUTF8StringEncoding];
tkStr = [deviceT stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"Device Token = %@",tkStr);
//UUID
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
NSString *uuidStr = ( NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);
CFRelease(uuid);
NSString *finalUIDstr = [uuidStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSLog(@"UUID = %@",finalUIDstr);
NSArray *keysArray = [NSArray arrayWithObjects:@"regid", @"uuid", nil];
NSArray *objectArray = [NSArray arrayWithObjects:tkStr,finalUIDstr, nil];
//Dictionary

NSDictionary *jsonDict = [NSDictionary dictionaryWithObjects:objectArray forKeys:keysArray];

最佳答案

如果要生成该JSON字符串,则可以构建字典(这是在Xcode 4.5中执行此操作的更简洁的方法,而不是构建这两个数组然后将它们组合成字典):

NSDictionary *dictionary = @{@"regid":tkStr,@"uuid":finalUIDstr};

然后生成您的JSON字符串:
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary
options:0
error:&error];

关于ios - json数据到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13748690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com