【发布时间】:2014-09-10 05:54:58
【问题描述】:
这是我从 API 得到的响应
{
Result = {
Area = (
"Shah-e-Alam",
Vastrapur,
"Nava Vadaj",
Ambavadi,
"Elis Bridge",
Ranip,
Gota,
);
};
Status = Success;
}
现在我只需要在数组中存储 7 个区域名称我如何存储它? 我的代码在这里
if ([[res valueForKey:@"Status"]isEqualToString:@"Success"]) {
NSLog(@"%@",res);
arrAreaname = [[res valueForKey:@"Result"] mutableCopy];
arrarea=[[arrAreaname valueForKey:@"Area"]mutableCopy];
NSLog(@"arrarea: %@", arrarea);
NSData *json = [NSJSONSerialization dataWithJSONObject:arrarea
options:NSJSONWritingPrettyPrinted error:nil];
NSLog(@"JSON: %@", json);
NSString *jsonString = [[NSString alloc] initWithData:json
encoding:NSUTF8StringEncoding];
NSLog(@"JSON string: %@", jsonString);
}else{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"ERROR"
message:@"connection error"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}
我最后来到这里的 jasonstring 现在我如何将数据存储在数组中?
【问题讨论】:
-
您可以在字典中添加数据,然后将该字典添加到数组中,例如 NSArray *Array = [[NSArray alloc] initWithArray:[dic objectForKey:@"Result"]];
-
使用类似 NSArray 的数组 *Array = [[NSArray alloc] initWithArray:[dic objectForKey:@"Result"]valueforKey:@"Area"]
-
你已经把你的购物车放在你的 JSON 马之前。如果上面的 NSLog 输出是
res,那么它已经被 NSJSONSerialization 解析了,你不需要再做一次。你所谓的arrarea是你的最终数组。