如何将json数组按位置保存到数组中?
How to save json array into array position by position?
我在尝试阅读以下 json(示例)数组时遇到了困难:
[
{
"id":"173",
"user":"44",
"value":"100.00"
},
{
"id":"158",
"user":"44",
"value":"1000.00"
}
]
代码:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary * parameters = @{@"entitty":self.entity};
NSString *url = [[defaults objectForKey:@"url_api"] stringByAppendingString:@"/blabla"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary* json_string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSDictionary *json_dict = (NSDictionary *)json_string;
//it does not fail but is copying the same information as json_dict and what I actually need is to save position by position
NSArray * array = json_dict;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
请帮忙。
使用 AFJSONResponseSerializer 而不是 AFHTTPResponseSerializer,您将在 responseObject
.
中收到已解析的 NSArray 或 NSDictionary
我在尝试阅读以下 json(示例)数组时遇到了困难:
[
{
"id":"173",
"user":"44",
"value":"100.00"
},
{
"id":"158",
"user":"44",
"value":"1000.00"
}
]
代码:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary * parameters = @{@"entitty":self.entity};
NSString *url = [[defaults objectForKey:@"url_api"] stringByAppendingString:@"/blabla"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary* json_string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSDictionary *json_dict = (NSDictionary *)json_string;
//it does not fail but is copying the same information as json_dict and what I actually need is to save position by position
NSArray * array = json_dict;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
请帮忙。
使用 AFJSONResponseSerializer 而不是 AFHTTPResponseSerializer,您将在 responseObject
.