zoukankan      html  css  js  c++  java
  • JSONXML

    1.json

    • 先读根节点.可以是第三方[[[NSStringalloc]initWithData:data encoding:4] JSONValue];__________________ 也可以系统的[NSJSONSerialization   SONObjectWithData:data    options:NSJSONReadingAllowFragments      error:nil]
    • 接着获取所需的内容.

    2.xml

    • 先获取文件所有内容.GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:data options:0 error:&error];
    • 再获取根节点.GDataXMLElement *rootElement = [doc rootElement];
    • 最后获取所需的内容.[rootElement nodesForXPath:@"//response/docList/docInfo/docImg" error:&error];

    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(基于通知模式,边读边处理)http://www.cnblogs.com/WLL-Hero/p/3568914.html

    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;
    }
  • 相关阅读:
    github提交用户权限被拒
    vue数据响应式的一些注意点
    总结一下做移动端项目遇到的坑
    react-router
    promise-async-await
    递归函数
    Linux基础
    所有的数据处理都是map-reduce
    Mac下配置JAVA_HOME
    MySQL高级
  • 原文地址:https://www.cnblogs.com/huen/p/3560870.html
Copyright © 2011-2022 走看看