我需要使用AFNetworking
构建这样的网址,问题是{
和}
如何通过parameter
/api/sth.json?filter[condition]={"53891":[123],"53892":[123,124]}
所以我的代码看起来像这样(我更简单):
[self GET:myUrl parameters:@{
@"filter" : @{
@"condition" : @{
@"53891" : @[@(123)],
@"53892" : @[@(123),@(124)]}
},
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
success(operation,responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(operation,error);
}];
但它产生的不是预期的输出:
/api/sth.json?filter[condition][53891][]=123&filter[condition][53892][]=123&filter[condition][53892][]=124
有一种方法可以在parameters
的{{1}}中执行此操作,也可以手动将其放入字符串中?
修改
我目前的解决方案是这样的:
AFHTTPRequestOperation
输入字典是:
+(NSString*)convertFromDictionary:(NSDictionary*)dic {
NSMutableString *outputStr = [NSMutableString new];
[outputStr appendString:@"{"];
NSArray *allKeys = [[dic allKeys] sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:nil ascending:NO]]];
for(NSString *key in allKeys) {
NSArray *objects = dic[key];
[outputStr appendString:[NSString stringWithFormat:@"\"%@\":[",key]];
for(NSNumber *nb in objects) {
[outputStr appendString:[NSString stringWithFormat:@"%li",[nb longValue]]];
if(![nb isEqual:[objects lastObject]]) {
[outputStr appendString:@","];
} else {
[outputStr appendString:@"]"];
}
}
if(![key isEqual:[allKeys lastObject]]) {
[outputStr appendString:@","];
}
}
[outputStr appendString:@"}"];
return outputStr;
}
但它只不过是字符串比较。没有更聪明的方法可以直接使用AFNetworking实现它,因为它是相当标准的URL参数?
答案 0 :(得分:1)
你需要这样做:
NSData
将字典转换为NSJsonSerialization
,然后将该NSData对象转换为NSString)答案 1 :(得分:1)
您需要两种不同的解析字典的方法,而这不能自动完成。正如Mehul之前所说,尝试序列化你的参数" condition" (在创建"参数之前将其值转换为字符串)#34;词典:
NSError *error;
NSDictionary *dict = @{@"53891" : @[@(123)], @"53892" : @[@(123),@(124)]};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
NSString *params = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[self GET:myUrl parameters:@{
@"filter" : @{
@"condition" : params
},
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
success(operation,responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(operation,error);
}];
答案 2 :(得分:0)
用括号括起数字是def get_filament_value(self, fileName):
# ...
if re_value:
return float(re_value.group(1))
else:
return 0.0
def read_gcode(self):
root.fileName = filedialog.askopenfilename(filetypes = (("GCODE files", "*.gcode"), ("All files", "*.*")))
volume = self.get_filament_value(root.fileName)
mass = volume * 1.13
self.volume_text.set('Volume is {}'.format(volume))
self.mass_text.set('Mass is {}'.format(mass))
def init_window(self):
# ...
self.volume_text = StringVar()
self.mass_text = StringVar()
volume_label = Label(self, justify=CENTER, compound=BOTTOM, textvariable=self.volume_text)
volume_label.pack()
mass_label = Label(self, justify=CENTER, compound=BOTTOM, textvariable=self.mass_text)
mass_label.pack()
# ...
:
expressions
阅读优秀解释https://stackoverflow.com/a/9349981/3096087
也许用括号来包裹AFHTTPRequestOperation。
@(6 + x * 2012)
通过以下方式直接声明:
NSNumber
所以尝试使用输入字典,方法如下:
NSNumber *number = @123;