0

我有一个 json http://gdata.youtube.com/feeds/api/users/ruflixnet/playlists?v=2&alt=jsonc ,我想获取 id、title 和 size,但现在不知道该怎么做。

4

4 回答 4

2

我推荐JSONKit(查看基准)

一个非常高性能的 Objective-C JSON 库

解析

在此处输入图像描述

于 2012-06-04T08:36:07.953 回答
1

例如,使用 SBJSON 库,我一直在使用它们,它们非常好。

你在这里有一个教程:

http://blog.zachwaugh.com/post/309924609/how-to-use-json-in-cocoaobjective-c

于 2012-06-04T08:32:55.190 回答
1

下载 JSON 库并将其包含在您的项目中,而不是为您的数据创建以下类 Make NSObject 类

对象数据.h

  @interface ObjectData : NSObject {

NSString *id; 
NSString *title;
NSString *size;

  }
@property(nonatomic,retain) NSString *id;
@property(nonatomic,retain) NSString *title; 
@property(nonatomic,retain) NSString *size;
@end

对象数据.M

   #import ObjectData.h

  @implementation ObjectData

  @synthesize id; 
  @synthesize title;
  @synthesize size;
  @end

制作另一个 DataController 类

  #import <Foundation/Foundation.h>


  @interface DataController : NSObject {

  }

  + (id)staticVersion;
 - (NSMutableArray *) startParsing:(NSString *)theURLString;
  end


 #import "DataController.h"
 @implementation DataController
 DataController *theInstance;

  +(id)staticVersion
                            {
if(!theInstance){
    theInstance = [[DataController alloc] init];
}
return theInstance;
   }



   - (NSMutableArray *) startParsing:(NSString *)theURLString {

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",theURLString]];
NSString *fileContent= [NSString stringWithContentsOfURL:url];
SBJSON *parser = [[SBJSON alloc] init];  
NSDictionary *data = (NSDictionary *) [parser objectWithString:fileContent error:nil];  
NSArray *items = (NSArray *) data ;  
return items;
    }

   @end

并在视图中加载了

  NSArray *tempArray =[[DataController staticVersion] startParsing:serverName];

   for (int i = 0; i<[tempArray count]; i++) {
id *item = [tempArray objectAtIndex:i];
NSDictionary *dict = (NSDictionary *) item;

ObjectData *theObject =[[ObjectData alloc] init];
[theObject setid:[dict objectForKey:@"id"]];
[theObject settitle:[dict objectForKey:@"title"]];  
[theObject setsize:[dict objectForKey:@"size"]];



[resultArray addObject:theObject];
[theObject release];
theObject=nil;

最后制作preferenc.h文件并给出你的json数据路径

  #import <Foundation/Foundation.h>

  #define serverName @"http://gdata.youtube.com/feeds/api/users/ruflixnet/playlists?v=2&alt=jsonc"

希望这能正常工作

于 2012-06-04T11:39:34.293 回答
0

使用 SBJson

SBJsonParser *parser = [[SBJsonParser alloc] init];

NSMutableDictionary *dicRes = [parser objectWithString:stringFromServer error:nil];
于 2012-06-04T08:40:58.290 回答