1

我正在开发一个应用程序,我必须在其中以如下格式发送deviceTokenuuid服务器JSON"regid":"x1y2z3","uuid":"1a2b3c"如何将NSStringsNSData以这种格式存储并将其发送到服务器?

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];
4

2 回答 2

2

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

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

然后生成您的 JSON 字符串:

NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary
                                               options:0
                                                 error:&error];
于 2012-12-06T17:17:02.427 回答
1

将 NSDictionary 转换为 Json(NSJSONSerialization 或 SBJson)并将其作为 Application/Json 发布到服务器。

于 2012-12-06T17:16:50.067 回答