我是 ios 及其开发的新手。我有正确设置的干净代码 AFNetworking
。我的基本 URl 的 json Encording 有 JSON 对象和数组以及值。在我的 JSON 输出中,我想在每次获取 Null 时获取“缩略图”的值。请帮助我获取我的 json 输出的“名称、缩略图、id、图像”。请找到我的 NSDictionary 类型打印对象的 NSlog。
2014-07-20 09:08:33.110 WADTourisum[1157:60b] Reachability Flag Status: -R ------- networkStatusForFlags
2014-07-20 09:08:33.879 WADTourisum[1157:60b] JSON: {
Main = (
{
id = 1;
"image_bundle_id" = 1;
images = (
"http://wearedesigners.net/clients/clients12/tourism/images/guides/oceans/slide_images/1.jpg",
"http://wearedesigners.net/clients/clients12/tourism/images/guides/oceans/slide_images/2.jpg",
"http://wearedesigners.net/clients/clients12/tourism/images/guides/oceans/slide_images/3.jpg"
);
name = OCEAN;
thumbnail = "http://wearedesigners.net/clients/clients12/tourism/images/guides/thumbs/ocean.jpg";
},
{
id = 2;
"image_bundle_id" = 23;
images = (
"http://wearedesigners.net/clients/clients12/tourism/images/guides/heritages/slide_images/1.jpg",
"http://wearedesigners.net/clients/clients12/tourism/images/guides/heritages/slide_images/2.jpg",
"http://wearedesigners.net/clients/clients12/tourism/images/guides/heritages/slide_images/3.png"
);
name = Heritage;
thumbnail = "http://wearedesigners.net/clients/clients12/tourism/images/guides/thumbs/heritage.jpg";
},
{
id = 3;
"image_bundle_id" = 0;
images = (
);
name = "Tea Country";
thumbnail = "http://wearedesigners.net/clients/clients12/tourism/images/guides/thumbs/teaCountry.jpg";
},
{
id = 4;
"image_bundle_id" = 0;
images = (
);
name = "WILD LIFE";
thumbnail = "http://wearedesigners.net/clients/clients12/tourism/images/guides/thumbs/wildLife.jpg";
},
{
id = 5;
"image_bundle_id" = 0;
images = (
);
name = Culture;
thumbnail = "http://wearedesigners.net/clients/clients12/tourism/images/guides/thumbs/culture.jpg";
},
{
id = 6;
"image_bundle_id" = 0;
images = (
);
name = "NIGHT LIFE";
thumbnail = "http://wearedesigners.net/clients/clients12/tourism/images/guides/thumbs/nightLife.jpg";
}
);
}
我的代码片段
-(void) retriveData
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET"http://www.fr20.wearedesigners.net/WADMac/tourism/fetchGuideListAndroid.php"
parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.posts =(NSDictionary *)responseObject;
self.post =self.posts[@"thumbnail"];
NSLog(@"JSON: %@", self.post);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle"lease log into internetet"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle"Ok"
otherButtonTitles:nil];
[alertView show];
}];
}
您收到了响应,我看到的唯一问题是您无法正确检索值。
在从 JSON 中获取数据时,请记住获取数据的格式,即获取数组或字典。
看到您的回复,您将获得本身包含字典的 Array。
使用以下代码获取值
NSArray *array = [responseObject valueForKey"Main"];
for (NSDictionary *dict in array) {
NSInteger ids = [[dict valueForKey"id"] integerValue];
NSString *name = [dict valueForKey"name"];
NSString *thumbnail = [dict valueForKey"thumbnail"];
NSArray *arrImages = [dict valueForKey"images"];
//You can use them accordingly
}
希望这对您有所帮助。快乐编码
关于ios - AFNetworking 获取 json 值并将其保存为 NSMutablearray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24850911/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |