先看下整个json数据的格式,如下:
{
"status": 200,
"msg": "成功读取",
"total": 4,
"list": [
{
"rid": "100001",
"name": "倪氏海泰",
"sofc": "testdata",
"pcc": "88",
"tastegrade": "1",
"envgrade": "2",
"servgrade": "3",
"recomm": "menu001 adfads dsfgsfe sdsfddasf sdfsf sdfsdf sdsfcsd efrfs",
"image": "http://www.dianxiaor.com/dicpics/100001/100001.jpg",
"address": "海泰大厦",
"lng": "116.372684",
"lat": "39.993992",
"distance": "0",
"discounts": [
{
"did": "1",
"discount": "5",
"stime": "01:00:00",
"etime": "04:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100001big/a.jpg"
},
{
"did": "0",
"discount": "5",
"stime": "01:00:00",
"etime": "04:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100001big/b.jpg"
},
{
"did": "2",
"discount": "6",
"stime": "05:00:00",
"etime": "07:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100001big/c.jpg"
},
{
"did": "0",
"discount": "6",
"stime": "05:00:00",
"etime": "07:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100001big/d.jpg"
},
{
"did": "3",
"discount": "7",
"stime": "08:00:00",
"etime": "10:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100001big/"
},
{
"did": "0",
"discount": "7",
"stime": "08:00:00",
"etime": "10:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100001big/"
},
{
"did": "4",
"discount": "8",
"stime": "21:00:00",
"etime": "23:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100001big/"
},
{
"did": "0",
"discount": "8",
"stime": "21:00:00",
"etime": "23:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100001big/"
}
]
},
{
"rid": "100002",
"name": "大东北",
"sofc": "testdata",
"pcc": "88",
"tastegrade": "1",
"envgrade": "2",
"servgrade": "3",
"recomm": "menu001 adfads dsfgsfe sdsfddasf sdfsf sdfsdf sdsfcsd efrfs",
"image": "http://www.dianxiaor.com/dicpics/100002/100002.jpg",
"address": "志新东路",
"lng": "116.375146",
"lat": "39.994365",
"distance": "213",
"discounts": [
{
"did": "1",
"discount": "5",
"stime": "01:00:00",
"etime": "02:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100002big/a.jpg"
},
{
"did": "0",
"discount": "5",
"stime": "01:00:00",
"etime": "02:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100002big/b.jpg"
},
{
"did": "2",
"discount": "6",
"stime": "03:00:00",
"etime": "04:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100002big/c.jpg"
},
{
"did": "0",
"discount": "6",
"stime": "03:00:00",
"etime": "04:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100002big/d.jpg"
},
{
"did": "3",
"discount": "7",
"stime": "08:00:00",
"etime": "10:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100002big/"
},
{
"did": "0",
"discount": "7",
"stime": "08:00:00",
"etime": "10:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100002big/"
},
{
"did": "4",
"discount": "8",
"stime": "21:00:00",
"etime": "23:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100002big/"
},
{
"did": "0",
"discount": "8",
"stime": "21:00:00",
"etime": "23:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100002big/"
}
]
},
{
"rid": "100003",
"name": "伊喜饺子",
"sofc": "testdata",
"pcc": "66",
"tastegrade": "1",
"envgrade": "2",
"servgrade": "3",
"recomm": "menu001 adfads dsfgsfe sdsfddasf sdfsf sdfsdf sdsfcsd efrfs",
"image": "http://www.dianxiaor.com/dicpics/100003/100003.jpg",
"address": "志新西路",
"lng": "116.371552",
"lat": "39.99612",
"distance": "255",
"discounts": [
{
"did": "1",
"discount": "5",
"stime": "01:00:00",
"etime": "04:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100003big/a.jpg"
},
{
"did": "0",
"discount": "5",
"stime": "01:00:00",
"etime": "04:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100003big/b.jpg"
},
{
"did": "2",
"discount": "6",
"stime": "05:00:00",
"etime": "06:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100003big/c.jpg"
},
{
"did": "0",
"discount": "6",
"stime": "05:00:00",
"etime": "06:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100003big/d.jpg"
},
{
"did": "3",
"discount": "7",
"stime": "08:00:00",
"etime": "09:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100003big/"
},
{
"did": "0",
"discount": "7",
"stime": "08:00:00",
"etime": "09:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100003big/"
},
{
"did": "4",
"discount": "8",
"stime": "21:00:00",
"etime": "23:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100003big/"
},
{
"did": "0",
"discount": "8",
"stime": "21:00:00",
"etime": "23:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100003big/"
}
]
},
{
"rid": "100004",
"name": "小街卤煮",
"sofc": "testdata",
"pcc": "66",
"tastegrade": "1",
"envgrade": "2",
"servgrade": "3",
"recomm": "menu001 adfads dsfgsfe sdsfddasf sdfsf sdfsdf sdsfcsd efrfs",
"image": "http://www.dianxiaor.com/dicpics/100004/100004.jpg",
"address": "志新西路",
"lng": "116.37272",
"lat": "39.997516",
"distance": "391",
"discounts": [
{
"did": "1",
"discount": "5",
"stime": "01:00:00",
"etime": "04:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100004big/a.jpg"
},
{
"did": "0",
"discount": "5",
"stime": "01:00:00",
"etime": "04:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100004big/b.jpg"
},
{
"did": "2",
"discount": "6",
"stime": "06:00:00",
"etime": "07:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100004big/c.jpg"
},
{
"did": "0",
"discount": "6",
"stime": "06:00:00",
"etime": "07:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100004big/d.jpg"
},
{
"did": "3",
"discount": "7",
"stime": "09:00:00",
"etime": "10:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100004big/"
},
{
"did": "0",
"discount": "7",
"stime": "09:00:00",
"etime": "10:00:00",
"available": 10,
"dimg": "http://www.dianxiaor.com/dicpics/100004big/"
},
{
"did": "4",
"discount": "8",
"stime": "00:00:00",
"etime": "00:00:00",
"available": 0,
"dimg": "http://www.dianxiaor.com/dicpics/100004big/"
}
]
}
]
}
工程结构图如下:
[img]
[/img]
1,先把SBJSON-Clssses这个文件夹导入工程里面
2,添加DataSource.h 与 DataSource.m到工程中
@interface DataSource : NSObject
//becarefull NSString is a object
+(id)dataSource:(NSString *)str;
+(id)dataSourceFromUrl:(NSString *)url;
@end
#import "JSON.h"
#import "DataSource.h"
@implementation DataSource
//解析本地json数据
+(id)dataSource:(NSString *)str
{
//获取文件路径
NSString * path = [[NSBundle mainBundle] pathForResource:str ofType:@""];
//获取数据
NSData * reader = [NSData dataWithContentsOfFile:path];
//转换成NSString
NSString * JSONString = [[NSString alloc] initWithData:reader encoding:NSUTF8StringEncoding];
return [JSONString JSONValue];
}
//解析网络json数据,传入url
+(id)dataSourceFromUrl:(NSString *)url{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
//获取数据
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//转换成NSString
NSString * JSONString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
return [JSONString JSONValue];
}
@end
3、调用,在ViewController.h添加如下代码:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
NSDictionary * myData;
}
@property (retain, nonatomic) NSDictionary * myData;
@end
4、在在ViewController.m添加如下代码:
//
// ViewController.m
// JsonTestCao
//
// Created by 张 志亮 on 13-1-3.
// Copyright (c) 2013年 张 志亮. All rights reserved.
//
#import "ViewController.h"
#import "DataSource.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize myData;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//这是模拟操作json解析,用的是本地的json数据
//获取数据源
// NSString *str = @"jsondata";
// myData = [DataSource dataSource:str];
// NSInteger numbers = [[myData valueForKey:[[myData allKeys] objectAtIndex:0]] count];
// NSString *sectionNanme = [[myData allKeys]objectAtIndex:1];
// NSString *name = [[myData valueForKey:sectionNanme]objectAtIndex:0];
// NSLog(@"%d",[myData count]);
// NSLog(@"%d",numbers);
// NSLog(@"%@",sectionNanme);
// NSLog(@"%@",name);
//获得url
NSString *url = @"http://www.dianxiaor.com/getrinfos.php?lng=116.372684&lat=39.993995#jsonNav_l";
//获得数据
myData = [DataSource dataSourceFromUrl:url];
NSLog(@"%d",[myData count]);
//获得status
NSString *status = [myData valueForKey:@"status"];
NSLog(@"%@",status);
//获得msg
NSString *msg = [myData valueForKey:@"msg"];
NSLog(@"%@",msg);
//获得list的长度
NSInteger countList = [[myData valueForKey:@"list"]count];
NSLog(@"%d",countList);
//获得rid
NSString *rid = [[[myData valueForKey:@"list"]objectAtIndex:0]valueForKey:@"rid"];
NSLog(@"%@",rid);
//获得address
NSString *address = [[[myData valueForKey:@"list"]objectAtIndex:0]valueForKey:@"address"];
NSLog(@"%@",address);
//获得discounts count 8
NSInteger discounts = [[[[myData valueForKey:@"list"]objectAtIndex:0]valueForKey:@"discounts"]count];
NSLog(@"discounts==%d",discounts);
//获得list第一项里面discounts里面所有的dimg
NSString *dimg = [[[[myData valueForKey:@"list"]objectAtIndex:0]valueForKey:@"discounts"]valueForKey:@"dimg"];
//this is all dimg for "objectAtIndex:0" and "discounts"
NSLog(@"dimg==%@",dimg);
//获得list第一项里面discounts里面,一共有8项 第一项的dimg的值
NSString *firstDimg = [[[[[myData valueForKey:@"list"]objectAtIndex:0]valueForKey:@"discounts"]objectAtIndex:0]valueForKey:@"dimg"];
NSLog(@"firstDimg=%@",firstDimg);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
运行程序,打印结果如下:
2013-01-04 10:42:26.691 JsonTestCao[476:c07] 4
2013-01-04 10:42:26.695 JsonTestCao[476:c07] 200
2013-01-04 10:42:26.695 JsonTestCao[476:c07] 成功读取
2013-01-04 10:42:26.695 JsonTestCao[476:c07] 4
2013-01-04 10:42:26.695 JsonTestCao[476:c07] 100001
2013-01-04 10:42:26.696 JsonTestCao[476:c07] 海泰大厦
2013-01-04 10:42:26.696 JsonTestCao[476:c07] discounts==8
2013-01-04 10:42:26.697 JsonTestCao[476:c07] dimg==(
"http://www.dianxiaor.com/dicpics/100001big/a.jpg",
"http://www.dianxiaor.com/dicpics/100001big/b.jpg",
"http://www.dianxiaor.com/dicpics/100001big/c.jpg",
"http://www.dianxiaor.com/dicpics/100001big/d.jpg",
"http://www.dianxiaor.com/dicpics/100001big/",
"http://www.dianxiaor.com/dicpics/100001big/",
"http://www.dianxiaor.com/dicpics/100001big/",
"http://www.dianxiaor.com/dicpics/100001big/"
)
2013-01-04 10:42:26.697 JsonTestCao[476:c07] firstDimg=http://www.dianxiaor.com/dicpics/100001big/a.jpg