[英]How to get a Dictionary Object from a Web Service
I'm using a web service that gives back JSON data of jobs. 我正在使用一个Web服务来提供作业的JSON数据。 I have called the service and displayed ALL the JSON data as such:
我调用了服务并显示了所有JSON数据:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlString = @"http://jobs.github.com/positions.json?description=python&location=new+york";
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSString *item = [json objectAtIndex:0];
NSLog(@"%@", item);
// Do any additional setup after loading the view.
} }
So this returns one job with TON of information such as "description", "company name", "location". 因此,这将返回一个具有TON信息的作业,例如“描述”,“公司名称”,“位置”。 I want to be able to get JUST location or JUST description.
我希望能够获得JUST位置或JUST描述。 How would I go about doing that?
我该怎么做呢?
All help is appreciated, thanks. 感谢所有帮助。
you get an array of dictionaries... replace 你得到一系列词典......替换
NSString *item = [json objectAtIndex:0]; //old
with 同
for(NSDictionary *item in jsonArray) {
NSLog(@"Item desc : %@", item[@"description"]);
}
Reading the following article will give you a good grounding on parsing JSON data. 阅读以下文章将为您提供解析JSON数据的良好基础。
http://www.intertech.com/Blog/basic-json-parsing-in-ios/ http://www.intertech.com/Blog/basic-json-parsing-in-ios/
In your case you can access it directly: 在您的情况下,您可以直接访问它:
NSDictionary *item = [json objectAtIndex:0];
NSString *location = [item objectForKey:@"location"];
NSLog(@"%@", location);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.