Objective C 从 .Net API 获取 MongoDB ObjectID 作为字符串,反之亦然
Objective C get MongoDB ObjectID from .Net API as string and vice versa
我必须使用 return 的 .Net API 并使用 MongoDB ObjectId 作为结果和参数
例如服务器return
_id": {
"Timestamp": 1478679428,
"Machine": 1031587,
"Pid": 9124,
"Increment": 2344035,
"CreationTime": "/Date(1478679428000)/"
}
以 NSDictionary
的形式表示 ObjectID 值 ObjectId("5822db840fbda323a423c463")
我的问题是:如何将上面的 NSDictionary
_id 转换为 Objective C 中的字符串值 5822db840fbda323a423c463
?
反之亦然,将 NSString
5822db840fbda323a423c463
转换为 NSDictionary
作为 api 参数发送 ?
注意:我不能改变api,只能消耗它
任何帮助将不胜感激
我post我的工作成果作为答案,希望对以后的人有帮助
用于将 NSDictionary
从 API 结果序列化为 NSString
- (NSString *)NSStringFromNSDictionary:(NSDictionary *)dict
{
long Timestamp = [[dict objectForKey:@"Timestamp"] longValue];
long Machine = [[dict objectForKey:@"Machine"] longValue];
long Pid = [[dict objectForKey:@"Pid"] longValue];
long Increment = [[dict objectForKey:@"Increment"] longValue];
NSString *oid = [NSString stringWithFormat:@"%08lx%06lx%04lx%06lx", Timestamp, Machine, Pid, Increment];
return oid;
}
用于将 NSString
序列化回 .Net Mongo ObjectId,格式为 NSDictionary
- (NSDictionary *)NSStringFromNSDictionary:(NSString *)oid
{
long Timestamp = strtol([[oid substringToIndex:8] UTF8String], NULL, 16);
long Machine = strtol([[oid substringWithRange:NSMakeRange(8,6)] UTF8String], NULL, 16);
long Pid = strtol([[oid substringWithRange:NSMakeRange(14,4)] UTF8String], NULL, 16);
long Increment = strtol([[oid substringFromIndex:18] UTF8String], NULL, 16);
NSDictionary * dict = @{@"Timestamp": [NSNumber numberWithLong:Timestamp]
,@"Machine": [NSNumber numberWithLong:Machine]
,@"Pid": [NSNumber numberWithLong:Pid]
,@"Increment": [NSNumber numberWithLong:Increment]
,@"CreationTime": [NSString stringWithFormat:@"/Date(%ld)/",Timestamp*1000]
};
return dict;
}
我必须使用 return 的 .Net API 并使用 MongoDB ObjectId 作为结果和参数
例如服务器return
_id": {
"Timestamp": 1478679428,
"Machine": 1031587,
"Pid": 9124,
"Increment": 2344035,
"CreationTime": "/Date(1478679428000)/"
}
以 NSDictionary
的形式表示 ObjectID 值 ObjectId("5822db840fbda323a423c463")
我的问题是:如何将上面的 NSDictionary
_id 转换为 Objective C 中的字符串值 5822db840fbda323a423c463
?
反之亦然,将 NSString
5822db840fbda323a423c463
转换为 NSDictionary
作为 api 参数发送 ?
注意:我不能改变api,只能消耗它
任何帮助将不胜感激
我post我的工作成果作为答案,希望对以后的人有帮助
用于将 NSDictionary
从 API 结果序列化为 NSString
- (NSString *)NSStringFromNSDictionary:(NSDictionary *)dict
{
long Timestamp = [[dict objectForKey:@"Timestamp"] longValue];
long Machine = [[dict objectForKey:@"Machine"] longValue];
long Pid = [[dict objectForKey:@"Pid"] longValue];
long Increment = [[dict objectForKey:@"Increment"] longValue];
NSString *oid = [NSString stringWithFormat:@"%08lx%06lx%04lx%06lx", Timestamp, Machine, Pid, Increment];
return oid;
}
用于将 NSString
序列化回 .Net Mongo ObjectId,格式为 NSDictionary
- (NSDictionary *)NSStringFromNSDictionary:(NSString *)oid
{
long Timestamp = strtol([[oid substringToIndex:8] UTF8String], NULL, 16);
long Machine = strtol([[oid substringWithRange:NSMakeRange(8,6)] UTF8String], NULL, 16);
long Pid = strtol([[oid substringWithRange:NSMakeRange(14,4)] UTF8String], NULL, 16);
long Increment = strtol([[oid substringFromIndex:18] UTF8String], NULL, 16);
NSDictionary * dict = @{@"Timestamp": [NSNumber numberWithLong:Timestamp]
,@"Machine": [NSNumber numberWithLong:Machine]
,@"Pid": [NSNumber numberWithLong:Pid]
,@"Increment": [NSNumber numberWithLong:Increment]
,@"CreationTime": [NSString stringWithFormat:@"/Date(%ld)/",Timestamp*1000]
};
return dict;
}