JSONparsing在iOS中使用NSJSONSerialization
我parsing我的代码中的JSON
。 但是在检索parsing的JSON
数据时,我遇到了一些意想不到的问题。 所以让我解释一下我的问题。
我必须使用xcodeparsing下面的JSON
数据。 在浏览器中input相同的URL时,这就是要parsing的数据:
{ "RESPONSE":[ {"id":"20", "username":"john", "email":"abc@gmail.com", "phone":"1234567890", "location":"31.000,71.000"}], "STATUS":"OK", "MESSAGE":"Here will be message" }
我的代码达到这个JSON
数据如下:
NSData *data = [NSData dataWithContentsOfURL:finalurl]; NSError *error; NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
如果我使用NSLog
打印对象json
,即NSLog(@"json: %@", [json description]);
看起来像:
json: { MESSAGE = "Here will be message"; RESPONSE =( { email = "abc@gmail.com"; id = 20; location = "31.000,71.000"; phone = 1234567890; username = john; } ); STATUS = OK; }
所以,我在这里观察了两件事情:
-
与上面的Web响应相比,密钥(或节点)的序列(
MESSAGE
,RESPONSE
和STATUS
)被改变。 -
RESPONSE
被包含在'(' & ')'
大括号中。
现在,如果我分离出键MESSAGE
& STATUS
和NsLog
值,那么它们将被打印为适当的string。
像msgStr:Here will be message
和Status:OK
但是,如果我将RESPONSE
值作为字典分离出来,并再次将字典的子值(例如email
和username
分开,则无法将其作为string获取 。
这里是我写的代码:
NSMutableDictionary *response = [json valueForKey:@"RESPONSE"]; NSString *username = [response valueForKey:@"username"]; NSString *emailId = [response valueForKey:@"email"];
如果我打印username
和emailId
,那么他们不会被打印为正常的string ,而是输出为:
username:( john ) email:( abc@gmail.com )
所以我的问题是为什么它不是一个正常的string? 如果我试图进一步使用这个variables,那么它们显示在'(' & ')'
括号内'(' & ')'
值。 这是由于NSJSONSerialization
发生?
首先在你的JSON
响应dictionary
,在' RESPONSE '键下,你有一个数组而不是一个dictionary
,该数组包含dictionary
对象。 所以要提取用户名和电子邮件ID ,如下所示
NSMutableDictionary *response = [[[json valueForKey:@"RESPONSE"] objectAtIndex:0]mutableCopy]; NSString *username = [response valueForKey:@"username"]; NSString *emailId = [response valueForKey:@"email"];
当你看到这样的大括号时,它代表一个数组,而不是一个字典。 你的JSON也显示了将数据括在括号内('['和']')。 所以:
RESPONSE =( { email = "abc@gmail.com"; id = 20; location = "31.000,71.000"; phone = 1234567890; username = john; } );
RESPONSE是一个字典数组。 要访问数据,遍历数组:
for (NSDictionary *responseDictionary in [JSONDictionary objectForKey:@"RESPONSE"]) { NSString *name = [responseDictionary objectForKey:@"username"]; ..... }
或者在索引处获取字典:
NSDictionary *responseDictionary = [[JSONDictionary objectForKey:@"RESPONSE"] objectAtIndex:0]; NSString *name = [responseDictionary objectForKey:@"username"];
每当有疑问时,logging下课程:
NSLog(@"%@", [[dictionary objectForKey:@"key"] class]);
查看字典中返回的内容。
1)与上面的Web响应相比,密钥(或节点)的序列(MESSAGE,RESPONSE和STATUS)被改变。
NSDictionary
的NSLog
是基于sorting的键。
2)RESPONSE被包含在'('&')'括号中。
RESPONSE =( { email = "abc@gmail.com"; id = 20; location = "31.000,71.000"; phone = 1234567890; username = john; } );
RESPONSE
是一个关键,值是一个NSArray
。 该数组包含一个NSDictionary
对象,其键为email,id,位置,电话和用户名。
注意:( )表示数组。 {字典说。
RESPONSE
包含一个不是对象的数组。
所以试试这样:
NSMutableArray *response = [json valueForKey:@"RESPONSE"]; NSString *username = [[response objectAtIndex:0] valueForKey:@"username"]; NSString *emailId = [[response objectAtIndex:0] valueForKey:@"email"]; NSLog(@"User=>[%@] Pwd=>[%@]",username ,emailId );
编译标记 – 检查可达性和parsing数据
NSString *urlString = [NSString stringWithFormat: @"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=11.021459,76.916332&radius=2000&types=atm&sensor=false&key=AIzaSyD7c1IID7zDCdcfpC69fC7CUqLjz50mcls"]; NSURL *url = [NSURL URLWithString: urlString]; NSData *data = [NSData dataWithContentsOfURL:url]; NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData: data options: 0 error: nil]; array = [[NSMutableArray alloc]init]; array = [[jsonData objectForKey:@"results"] mutableCopy]; [jsonTableview reloadData];}
编译标记 – UITableView委托
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return array.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellid = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid]; cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid]; cell.textLabel.text = [[array valueForKeyPath:@"name"]objectAtIndex:indexPath.row]; cell.detailTextLabel.text = [[array valueForKeyPath:@"vicinity"]objectAtIndex:indexPath.row]; NSURL *imgUrl = [NSURL URLWithString:[[array valueForKey:@"icon"]objectAtIndex:indexPath.row]]; NSData *imgData = [NSData dataWithContentsOfURL:imgUrl]; cell.imageView.layer.cornerRadius = cell.imageView.frame.size.width/2; cell.imageView.layer.masksToBounds = YES; cell.imageView.image = [UIImage imageWithData:imgData]; return cell; }
#import.h
@interface JsonViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> @property (weak, nonatomic) IBOutlet UITableView *jsonTableview; @property (weak, nonatomic) IBOutlet UIBarButtonItem *barButton; @property (strong,nonatomic)NSArray *array; @property NSInteger select;
通过序列化数据,你得到一个JSON对象。 Json对象和字典有点不同。
而不是使用:
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
使用:
NSDictionary *json = (NSDictionary*) data;