JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。
JSON简单来说就是JavaScript中得对象和数组
1.对象:对象在js中表示为“{}”扩起来的内容,数据结构为{key:value,key:value,...}的键值对结构,在面向对象的语言中,key为对象的属性,value为对应的属性值。整个属性值可以是数字、字符串、数组、对象。数据之间用逗号隔开。
2.数组:数组在js中是中括号“[]”括起来的内容,数据结构为["C++","Javascript","Java","vb"...],取值方式使用索引获取,字段值的类型可以为数字、字符串、数组、对象。数据之间用逗号隔开。
3.对象、数组2中组成结构就可以组成复杂的数据结构。其实就是嵌套字典和数组。
4.要使用JSON解析首先要导入JSON工具包。
#import "ViewController.h"
#import "JSON.h"
@interface ViewController ()
@end
@implementation ViewController
- (IBAction)jsonPs:(id)sender {
NSString *path = @"http://api.jiepang.com/v1/locations/search?lat=39.916&lon=116.393&count=2&source=100000";
NSURL *url = [[NSURL alloc] initWithString:path];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *dic = [string JSONValue];
NSLog(@"%@",dic);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[super dealloc];
}
@end