Iphone 需要解析JSON的帮助吗
我正在学习如何解析JSON。我已经完成了raywenderlich教程,但仍有一些步骤不清楚。我有自己的JSON:Iphone 需要解析JSON的帮助吗,iphone,ios,objective-c,json,Iphone,Ios,Objective C,Json,我正在学习如何解析JSON。我已经完成了raywenderlich教程,但仍有一些步骤不清楚。我有自己的JSON: { "Albumvideo":{ "album01":{ "titreAlbum":"Publicité", "photoAlbum":"blabla.jpg", "pubVideos":{ "pub01":[ {
{
"Albumvideo":{
"album01":{
"titreAlbum":"Publicité",
"photoAlbum":"blabla.jpg",
"pubVideos":{
"pub01":[
{
"titrePub":"Chauffage Compris",
"dureePub":"01'25''",
"photoPub":"chauffage.jpg",
"lienPub":"http://www.wmstudio.ch/videos/chauffage.mp4"
}
]
}
},
"album02":{
"titreAlbum":"Events",
"photoAlbum":"bloublou.jpg",
"eventsVideos":{
"event01":[
{
"titreEvent":"Chauffage Compris",
"dureeEvent":"01'25''",
"photoEvent":"chauffage.jpg",
"lienEvent":"http://www.wmstudio.ch/videos/chauffage.mp4"
}
]
}
}
}
}
我得到了解析JSON的“代码”:
- (void) viewDidLoad
{
[super viewDidLoad];
dispatch_async (kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:lienAlbumsVideo];
[self performSelectorOnMainThread:@selector(fetchedData:)withObject:data waitUntilDone:YES];
});
}
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSArray* albumsVideo = [json objectForKey:@"Albumvideo"];
NSLog(@"Nombre d'albums : %i",[albumsVideo count]);
}
这很好,我的NSLog返回“2”。我现在遇到的困难是制作一个带有“titreAlbum”或“event01”的数组。如果我这样做:
NSArray* event01 = [json objectForKey:@"event01"];
NSLog(@"Number of objects in event01 : %i ", [event01 count]);
我的NSLog返回“0”
我真的不明白如何在JSON中解析多维数组中的信息。谢谢,已经有了
尼古拉斯你没有二维数组。JSON不支持这一点,但支持数组的数组(就像C和Objective-C一样) (在Safari中输入)
你也可以使用KVC,如果你对中间层不感兴趣。
但是您的标识符和问题让我想一想,您的JSON的结构是错误的 有些事情, 每次你看到 {…} 在json中,它是NSDictionary
一旦解析,当
[……]
是NSArray
的开始和结束
因此,一旦您使用NSJSONSerialization
解析json,您就可以使用该知识导航该词典
给定json,您必须获得一个“titreAlbum”
数组,您必须执行以下操作:
NSDictionary *albumVideo = json[@"Albumvideo"];
NSMutableArray *albumTitres = [[NSMutableArray alloc] init];
for (NSDictionary *album in albumVideo) {
[albumTitres addObject:album[@"titreAlbum"]];
}
这就是说,我认为您的json并没有像通过JSONLint验证那样格式错误,但并不能帮助您解析它。我希望相册视频是一个相册数组,而不是相册字典。我也有同样的问题。我实际上是想进入我需要去的地方
{
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 23.0225066,
"lng" : 73.2544778
},
"southwest" : {
"lat" : 19.0718263,
"lng" : 72.57129549999999
}
},
"copyrights" : "Map data ©2014 Google",
"legs" : [
{
"distance" : {
"text" : "524 km",
"value" : 523839
},
"duration" : {
"text" : "7 hours 34 mins",
"value" : 27222
},
"end_address" : "Mumbai, Maharashtra, India",
"end_location" : {
"lat" : 19.0759856,
"lng" : 72.8776573
},
"start_address" : "Ahmedabad, Gujarat, India",
"start_location" : {
"lat" : 23.0225066,
"lng" : 72.57129549999999
},
"steps" : [
{
"distance" : {
"text" : "0.2 km",
"value" : 210
},
"duration" : {
"text" : "1 min",
"value" : 25
},
"end_location" : {
"lat" : 23.0226436,
"lng" : 72.573224
},
"html_instructions" : "Head on Swami Vivekananda RdRoad/Swami Vivekananda Rd",
"polyline" : {
"points" : "uqokCsa}yLS?GYEk@Cq@@qA@eA@aADm@"
},
"start_location" : {
"lat" : 23.0225066,
"lng" : 72.57129549999999
},
"travel_mode" : "DRIVING"
}]`
我需要访问routes.legs.steps.html\u说明
所以我的代码如下
NSURL *url=[[NSURL alloc] initWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=ahmedabad&destination=%@",self.city]];`
NSURLResponse *res;
NSError *err;
NSData *data=[NSURLConnection sendSynchronousRequest:[[NSURLRequest alloc] initWithURL:url] returningResponse:&res error:&err];
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSArray *routes=dic[@"routes"];
NSArray *legs=routes[0][@"legs"];
NSArray *steps=legs[0][@"steps"];
NSMutableArray *textsteps=[[NSMutableArray alloc] init];
NSMutableArray *latlong=[[NSMutableArray alloc]init];
for(int i=0; i< [steps count]; i++){
NSString *html=steps[i][@"html_instructions"];
[latlong addObject:steps[i][@"end_location"]];
[textsteps addObject:html];
}
NSURL*url=[[NSURL alloc]initWithString:[NSString stringWithFormat:@]http://maps.googleapis.com/maps/api/directions/json?origin=ahmedabad&destination=%@“,self.city]]`
NSURLResponse*res;
n错误*错误;
NSData*data=[NSURLConnection sendSynchronousRequest:[[NSURLRequest alloc]initWithURL:url]returningResponse:&res error:&err];
NSDictionary*dic=[NSJSONSerialization JSONObjectWithData:数据选项:NSJSONReadingMutableContainers错误:nil];
NSArray*routes=dic[@“routes”];
NSArray*legs=路由[0][@“legs”];
NSArray*步数=腿[0][@“步数”];
NSMutableArray*textsteps=[[NSMutableArray alloc]init];
NSMutableArray*latlong=[[NSMutableArray alloc]init];
对于(int i=0;i<[步数];i++){
NSString*html=步骤[i][@“html_指令”];
[latlong addObject:steps[i][@“end_location”];
[textsteps addObject:html];
}
Albumvideo返回的是字典而不是数组。与在任何其他情况下从多维数组解析它的方式相同。这绝对是零差异。使用“titreAlbum”或“event01”创建数组的困难。你能详细解释一下吗?我已经把我的问题编辑得更精确了。谢谢!您是否有一些路径可以为我的JSON创建更好的结构?我认为,数组和字典不匹配。你是php开发人员吗?在Objective-C(和JSON)中,未命名(索引)的项列表是一个数组,而字典(JSON:objects)是一对键和值。例如,我不希望event01是一个数组。但是,当然,我没有关于您的格式和需求的详细信息。我很久以前就制作了php。我的应用程序需要:一个包含视频类别的表视图,然后是一个包含所选类别视频的详细视图。对于分类,我有“标题”和“图片”以及相应的视频列表。对于每个视频,我都有“标题”、“持续时间”、“图片”和“链接”。这就是为什么我把我的JSON制作成这样,但它没有真正的知识。你能告诉我在哪里使用数组和字典吗?Thank'sSo视频应该是一个数组。视频中的项目应该是一个字典(JSON:object)。在您的代码“pubVideos”中:{是一个字典,“pub01”:[是一个数组。如果我总结一下,例如:“Albumvideo”=数组,“album01”=字典,“pubVideos”=数组,“pub01”=字典。它准确吗?谢谢Emilio。因此,如果我理解,Albumvideo应该是一个包含两个对象(album01和album02)的数组album01和02应该是字典吗?我想是的,这将允许您以更合理的方式与json交互。但正如我所说,当前的json也是有效的。
NSURL *url=[[NSURL alloc] initWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=ahmedabad&destination=%@",self.city]];`
NSURLResponse *res;
NSError *err;
NSData *data=[NSURLConnection sendSynchronousRequest:[[NSURLRequest alloc] initWithURL:url] returningResponse:&res error:&err];
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSArray *routes=dic[@"routes"];
NSArray *legs=routes[0][@"legs"];
NSArray *steps=legs[0][@"steps"];
NSMutableArray *textsteps=[[NSMutableArray alloc] init];
NSMutableArray *latlong=[[NSMutableArray alloc]init];
for(int i=0; i< [steps count]; i++){
NSString *html=steps[i][@"html_instructions"];
[latlong addObject:steps[i][@"end_location"]];
[textsteps addObject:html];
}