如何在我的应用程序中解析/获取 JSON [关闭]
Posted
技术标签:
【中文标题】如何在我的应用程序中解析/获取 JSON [关闭]【英文标题】:How to parse/GET JSON in my App [closed] 【发布时间】:2014-02-03 06:37:42 【问题描述】:我有数据库(linkP)
如何解析上述链接中的所有数据并显示结果
【问题讨论】:
看看这里***.com/questions/5547311/… 使用 NSURLConnection 和 NSJSONSerialization。 (同时检查 NSData 类) How to parsing JSON object in iPhone SDK (XCode) using JSON-Framework 的可能副本 @user3264750 问题确实应该表明尝试并在出现问题时寻求帮助。 【参考方案1】:- (void)viewDidLoad
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://arproject.site90.com/jsonbuilding.php"]];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];
NSArray *array=[jsonArray objectForKey:@"result"];
for (int i=0; i<[array count]; i++)
NSLog(@"the building id==%@",[[array objectAtIndex:i]objectForKey:@"building_id"]);
NSLog(@"the building name==%@",[[array objectAtIndex:i]objectForKey:@"building_name"]);
NSLog(@"the latitude==%@",[[array objectAtIndex:i]objectForKey:@"latitude"]);
NSLog(@"the longitude==%@",[[array objectAtIndex:i]objectForKey:@"longitude"]);
NSLog(@"the picture==%@",[[array objectAtIndex:i]objectForKey:@"picture"]);
【讨论】:
在控制台中查看结果,我正在等待 感谢您的帮助!! :) 使用字面语法;[[array objectAtIndex:i]objectForKey:@"building_id"]
可以更简单的表达:array[i][@"building_id"]
gt,谢谢兄弟,我怀疑它很容易识别对象键【参考方案2】:
dispatch_async(kBgQueue, ^
NSURL *myURL = [NSURL URLWithString:@"http://arproject.site90.com/jsonbuilding.php"];
NSData *data=[NSData dataWithContentsOfURL:myURL];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
);
-(void)fetchedData : (NSData*)responseData
NSError *error;
//parsing json data
NSMutableArray *dataArray;
NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
if(error == nil)
dataArray =[dictionary valueForKey:@"result"];
for (int i=0; i<[dataArray count]; i++)
NSLog(@"%@",dataArray);
if(error)
NSLog(@"Error is %@",[error localizedDescription]);
【讨论】:
以上是关于如何在我的应用程序中解析/获取 JSON [关闭]的主要内容,如果未能解决你的问题,请参考以下文章