首页 > Web开发 > 详细

JSON\XML

时间:2014-04-02 11:21:29      阅读:584      评论:0      收藏:0      [点我收藏+]

1.

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

也可以系统的[NSJSONSerialization

                         JSONObjectWithData:data

                         options:NSJSONReadingAllowFragments

                         error:nil]

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

bubuko.com,布布扣
{
    "name":"jobs",
    "age":38,
    "gender":true,
    "children":["bill","james","ben"],
    "wife":{"name":"sala","age":35}
}
bubuko.com,布布扣

第三方类库解析SBJSON

bubuko.com,布布扣
#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;
}
bubuko.com,布布扣

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

bubuko.com,布布扣
<students>
    <student>
            <name>张三</name>
            <age>24</age>
            <sex></sex>
    </student>
    <student>
            <name>李四</name>
            <age>21</age>
            <sex></sex>
    </student>

</students>
bubuko.com,布布扣

第三方.GDataXMLNode需写两个地方

bubuko.com,布布扣

bubuko.com,布布扣

XML解析

bubuko.com,布布扣
#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;
}
bubuko.com,布布扣

JSON\XML,布布扣,bubuko.com

JSON\XML

原文:http://www.cnblogs.com/huen/p/3560870.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!