如何在目标C中解析JSON格式 [英] How To Parse JSON Format in Objective C

查看:141
本文介绍了如何在目标C中解析JSON格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在编写一个iphone应用程序,以便将google搜索结果输入到我的应用程序中,……,我使用JSON类获取结果...当我在JSON解析器中对其进行解析并将其存储在NSDictionary中时,我得到了3个键:

Hey every one i am programming an iphone app to get google search results into my app ,,, i have used the JSON Class to get the result ... when i parsed it in JSON Parser and store it in NSDictionary i got 3 keys :

  1. responseData
  2. responseDetails
  3. responseStatus

重要的是第一个具有搜索结果的responseData ...

the important one is the first one responseData which is has the search results ...

问题(我认为)在responseData中有另一个键是结果",其中包含URL和其他内容,这对于我的应用程序来说是最重要的部分...如何访问此键并将其放入NSDictionary .....

the problem that there is (i think) another key within responseData which is "results" which contains the urls and other stuffs which is the most important part for my app... how to access this one and put it into NSDictionary .....

这是请求:

http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Paris%20Hilton

为了使事情更清楚,请考虑将请求放入浏览器,并在获取结果时将其复制并放在左侧的此网站中,以查看按键和其他内容:

and to make things clear please consider to put that request into your browser and when you get the results copy it and put it in this website at the left side to see what the keys and other things:

http://json.parser.online.fr/

thnx

推荐答案

您可以使用 JSON解析器-SB Json 将JSON字符串转换为ObjectiveC对象.请注意,ObjectiveC中提供了许多JSON解析器,但是我选择SB Json是因为它易于使用.但是根据一些基准,JSONKit比SBJson更快.

You could use JSON parser - SB Json to convert json string into ObjectiveC objects. Note that there are a number of JSON parsers available in ObjectiveC but I chose SB Json for it's ease of usage. But according to some benchmarks JSONKit is faster than SBJson.

一旦您拥有json字符串,就这样使用它-

Once you have your json string use this like so -

#import "JSON.h"

// Create SBJSON object to parse JSON
SBJSON *parser = [[SBJSON alloc] init];

// parse the JSON string into an object - assuming json_string is a NSString of JSON data
NSDictionary *object = [parser objectWithString:json_string error:nil];
NSLog(@"JSON data: %@", object);

如果需要将Twitter的公共时间轴解析为JSON,这就是您要做的事情.相同的逻辑也可以应用于您的Google搜索结果.您需要仔细检查所有的json结构...

Here's what you would do if you needed to parse public timeline from Twitter as JSON.The same logic could be applied to your Google Search results. You need to carefully inspect your json structure that's all...

// Create new SBJSON parser object
SBJSON *parser = [[SBJSON alloc] init];

// Prepare URL request to download statuses from Twitter
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://twitter.com/statuses/public_timeline.json"]];

// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

// parse the JSON response into an object
// Here we're using NSArray since we're parsing an array of JSON status objects
NSArray *statuses = [parser objectWithString:json_string error:nil];

// Each element in statuses is a single status
// represented as a NSDictionary
for (NSDictionary *status in statuses)
{
  // You can retrieve individual values using objectForKey on the status NSDictionary
  // This will print the tweet and username to the console
  NSLog(@"%@ - %@", [status objectForKey:@"text"], [[status objectForKey:@"user"] objectForKey:@"screen_name"]);
}

这篇关于如何在目标C中解析JSON格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆