JSON\XML

1.

先读根节点,可以是第三方[[[NSStringalloc]initWithData:data encoding:4] JSONValue];

也可以系统的[NSJSONSerialization

                         JSONObjectWithData:data

                         options:NSJSONReadingAllowFragments

                         error:nil]

json结构
原则:花括号的JSON对象解析为NSDictionary对象,中括号或小括号的JSON对象解析为NSArray对象,

JSON\XML
{
    "name":"jobs",
    "age":38,
    "gender":true,
    "children":["bill","james","ben"],
    "wife":{"name":"sala","age":35}
}
JSON\XML

第三方类库解析SBJSON

JSON\XML
#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;
}
JSON\XML

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

JSON\XML
<students>
    <student>
            <name>张三</name>
            <age>24</age>
            <sex></sex>
    </student>
    <student>
            <name>李四</name>
            <age>21</age>
            <sex></sex>
    </student>

</students>
JSON\XML

第三方.GDataXMLNode需写两个地方

JSON\XML

JSON\XML

XML解析

JSON\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;
}
JSON\XML

JSON\XML,布布扣,bubuko.com

JSON\XML

上一篇:洛谷 P1803题解 java 贪心


下一篇:webpack配置-devServer详细配置-proxy跨域(vue+react都可以使用的跨域代理方式)