JSON\XML
1.
先读根节点,可以是第三方[[[NSStringalloc]initWithData:data encoding:4] JSONValue];
也可以系统的[NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:nil]
json结构
原则:花括号的JSON对象解析为NSDictionary对象,中括号或小括号的JSON对象解析为NSArray对象,
{ "name":"jobs", "age":38, "gender":true, "children":["bill","james","ben"], "wife":{"name":"sala","age":35} }
第三方类库解析SBJSON
#import "AppDelegate.h" #import "JSON.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; #pragma mark -本地json解析- NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"sb" ofType:@"json"]; NSString *content = [NSString stringWithContentsOfFile:jsonPath encoding:4 error:nil]; NSArray *arr = [content JSONValue]; // NSLog(@"__________%@",arr); #pragma mark -网络系统json ()[]都是数组- NSURL *url = [NSURL URLWithString:@"http://www.chazidian.com/service/bushou/12/1/5"]; NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.f]; NSHTTPURLResponse *httpResponse= nil; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&httpResponse error:nil]; //第三方sbjson NSString *str = [[NSString alloc]initWithData:data encoding:4]; NSDictionary *dic = [str JSONValue]; /* //系统josn NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; */ NSLog(@"_________dic = %@",dic); #pragma mark -读- NSDictionary *dataDic = [dic objectForKey:@"data"]; // NSDictionary *pageDic = [dataDic objectForKey:@"page"]; NSArray *wordsArr = [dataDic objectForKey:@"words"]; for (NSDictionary *wordDic in wordsArr) { NSLog(@"%@ %@ %@",[wordDic objectForKey:@"simp"],[[wordDic objectForKey:@"yin"] objectForKey:@"pinyin"],[wordDic objectForKey:@"seq"]); } [self.window makeKeyAndVisible]; return YES; }
2
XML
xml有2种,dom/sax(基于通知模式,边读边处理)
XML是一种元语言(标记语言,成对存在),可以生产html.
只有1个根元素,元素中可以多个属性(<child name = "jobs" age = "15"><hobby>football</hobby></child> ),属性虽然可以扩充元素的信息,但建议尽量不使用属性,而将属性改为子元素(hobby).
<![CDATA <Student></Student>]>所标识的会被xml解析所忽略
- xml只有1个根节点
- 1个节点由标签对组成
- 1个节点有多个属性
test.xml
<students> <student> <name>张三</name> <age>24</age> <sex>男</sex> </student> <student> <name>李四</name> <age>21</age> <sex>男</sex> </student> </students>
第三方.GDataXMLNode需写两个地方
XML解析
#import "AppDelegate.h" #import "GDataXMLNode.h" #import "RootViewController.h" #define API_URL @"http://api.hudong.com/iphonexml.do?type=focus-c" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; #pragma mark -解析本地xml文件- /* NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"xml"]; NSError *error = nil; NSString *content = [NSString stringWithContentsOfFile:path encoding:4 error:&error]; if (content == nil) { NSLog(@"errorcode = ____________%d,%@,%@",error.code,error.description,error.domain); } // GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithXMLString:content options:0 error:&error];//解析,得到所有文本 GDataXMLElement *rootElement = [doc rootElement];//取根节点 NSArray *childArray = [rootElement nodesForXPath:@"//students/student/name" error:&error];//得到孩子节点(数组) // NSLog(@"_____________%@",childArray); for (int i = 0; i < childArray.count; i++) { // NSString *name = [childArray objectAtIndex:i]; // NSLog(@"____________name = %@",name); GDataXMLElement *studentName = [childArray objectAtIndex:i]; NSLog(@"__________studentName = %@",studentName.stringValue); } */ #pragma mark -解析网络xml- NSURL *url = [NSURL URLWithString:API_URL]; NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.f]; NSHTTPURLResponse *httpResponse = nil; NSError *error = nil; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&httpResponse error: &error]; NSLog(@"____________%@",[[NSString alloc]initWithData:data encoding:4]); GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:data options:0 error:&error]; GDataXMLElement *rootElement = [doc rootElement];//根节点 NSArray *ImageArr = [rootElement nodesForXPath:@"//response/docList/docInfo/docImg" error:&error];//关键 for (GDataXMLElement *ele in ImageArr) { // NSLog(@"%@",ele.stringValue); } [self.window makeKeyAndVisible]; return YES; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。