我想解析 NSString 中的 json 数据我该怎么做
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"%@",data);
NSArray *tempArray =[[DataController staticVersion] startParsing:data];
for (int i = 0; i<[tempArray count]; i++) {
id *item = [tempArray objectAtIndex:i];
NSDictionary *dict = (NSDictionary *) item;
SearchCode *theObject =[[SearchCode alloc] init];
[theObject setCodeValue:[dict objectForKey:@"CodeValue"]];
[theObject setCodeDescription:[dict objectForKey:@"CodeAddedDate"]];
[theObject setCodeAddedDate:[dict objectForKey:@"CodeAddedDate"]];
[theObject setCodeID:[dict objectForKey:@"CodeID"]];
[theObject setUpdateDateTime:[dict objectForKey:@"UpdateDateTime"]];
[cptArray addObject:theObject];
[theObject release];
theObject=nil;
}
数据控制器类
@interface DataController : NSObject {
}
+ (id)staticVersion;
- (NSMutableArray *) startParsing:(NSString *)theURLString;
@end
#import "DataController.h"
#import "JSON.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