ios XML,JSON,数组解析并转换成NSMutableArray(List(cl

页面导航:首页 > 网络编程 > JavaScript > ios XML,JSON,数组解析并转换成NSMutableArray(List(cl

ios XML,JSON,数组解析并转换成NSMutableArray(List(cl

来源: 作者: 时间:2016-02-05 11:06 【

一、简介JSON解析:通过正则将JSON([{ },{ } ])分解成多个包含实体实例内容的一小节({ }),在一小节中通过Runtime(运行时)()将实体所有属性 20540;找到并赋 20540;(正则查找实现)到实例({ } --

一、简介

JSON解析:

通过正则将JSON([{...},{...}...])分解成多个包含实体实例内容的一小节({...}),在一小节中通过Runtime(运行时)()将实体所有属性值找到并赋值(正则查找实现)到实例({...} --> 实体的一个实例),遍历所有小节({...})就将XML转换成NSMutableArray(类似:List)。

XML 解析:

通过GDataXml(也可以通过正则)将XML分解成多个包含实体实例内容的一小节XML,在一小节中通过Runtime(运行时)()将实体所有属性值找到并赋值(正则查找实现)到实例,遍历所有小节就将XML转换成NSMutableArray(类似:List)。

数组解析:

将XML拼接,通过GDataXml将XML转换成NSMutableArray(类似:List)(整个过程也可以通过正则实现)。

已将3个解析方法写到一个公用类(GlobalApplication)中

需注意的是实现方法中要添加2个头文件

#import 
#import "GDataXMLNode.h"

使用说明:

    // User 为一个 实体类
    // xml --> NSMutableArray (List)
    NSMutableArray *retVal = [GlobalApplication jsonToArray:xml class:User.class];
    // xml --> NSMutableArray (List)
    NSMutableArray *retVal = [GlobalApplication xmlToArray:xml class:User.class rowRootName:@"row"];
    // xml --> NSMutableArray (List)
    NSMutableArray *retVal = [GlobalApplication xmlToArray:xml];

二、代码

1、GlobalApplication.h

//
//  GlobalApplication.h
//  WebSerieBySoap
//
//  Created by fengs on 14-11-19.
//  Copyright (c) 2014年 fengs. All rights reserved.
//

#import 

@interface GlobalApplication : NSObject

#pragma mark -
#pragma mark - 将xml(数组)转换成NSMutableArray (List)
/**
 * 将xml(数组)转换成NSMutableArray
 * @param xml
     fs
     fs
     ...
 * @return NSMutableArray (List)
 */
+(NSMutableArray*)xmlToArray:(NSString*)xml;

#pragma mark -
#pragma mark - 将标准的xml(实体)转换成NSMutableArray (List)
/**
 * 把标准的xml(实体)转换成NSMutableArray
 * @param xml:
     
     ff0f0704fs
     ff0f0704fs
     ......
     
 * @param class:
    User
 * @param rowRootName:
    row
 * @return NSMutableArray (List)
 */
+(NSMutableArray*)xmlToArray:(NSString*)xml class:(Class)class rowRootName:rowRootName;

#pragma mark -
#pragma mark - 将标准的Json(实体)转换成NSMutableArray (List)
/**
 * 把标准的xml(实体)转换成NSMutableArray
 * @param xml:
    [{"UserID":"ff0f0704","UserName":"fs"},
    {"UserID":"ff0f0704","UserName":"fs"},...]
 * @param class:
    User
 * @return NSMutableArray (List)
 */
+(NSMutableArray*)jsonToArray:(NSString*)json class:(Class)class;
@end

2、GlobalApplication.m

//
//  GlobalApplication.m
//  WebServcieBySoap
//
//  Created by fengs on 14-11-19.
//  Copyright (c) 2014年 fengs. All rights reserved.
//

#import "GlobalApplication.h"
#import 
#import "GDataXMLNode.h"

@implementation GlobalApplication

#pragma mark - 
#pragma mark - 将xml(数组)转换成NSMutableArray (List)
/**
 * 将xml(数组)转换成NSMutableArray
 * @param xml
     fs
     fs
     ...
 * @return NSMutableArray (List)
 */
+(NSMutableArray*)xmlToArray:(NSString*)xml{
    
    NSMutableArray *retVal = [[[NSMutableArray alloc] init] autorelease];
    xml = [NSString stringWithFormat:@"%@",xml];
    GDataXMLDocument *root = [[[GDataXMLDocument alloc] initWithXMLString:xml options:0 error:nil] autorelease];
    GDataXMLElement *rootEle = [root rootElement];
    for (int i=0; i <[rootEle childCount]; i++) {
        GDataXMLNode *item = [rootEle childAtIndex:i];
        [retVal addObject:item.stringValue];
    }
    return retVal;
}

#pragma mark -
#pragma mark - 将标准的xml(实体)转换成NSMutableArray (List)
/**
 * 将标准的xml(实体)转换成NSMutableArray
 * @param xml:
     
     ff0f0704fs
     ff0f0704fs
     ......
     
 * @param class:
     User
 * @param rowRootName:
     row
 * @return NSMutableArray (List)
 */
+(NSMutableArray*)xmlToArray:(NSString*)xml class:(Class)class rowRootName:rowRootName{
    
    NSMutableArray *retVal = [[[NSMutableArray alloc] init] autorelease];
    GDataXMLDocument *root = [[[GDataXMLDocument alloc] initWithXMLString:xml options:0 error:nil] autorelease];
    GDataXMLElement *rootEle = [root rootElement];
    NSArray *rows = [rootEle elementsForName:rowRootName];
    for (GDataXMLElement *row in rows) {
        id object = [[class alloc] init];
        object = [self initWithXMLString:row.XMLString object:object];
        [retVal addObject:object];
        [object release];
    }
    return retVal;
}

/**
 * 将传递过来的实体赋值
 * @param xml(忽略实体属性大小写差异):
    ff0f0704fs
 * @param class:
    User @property userName,userID;
 * @return class
 */
+(id)initWithXMLString:(NSString*)xml object:(id)object{
    
    unsigned int outCount, i;
    objc_property_t *properties = class_copyPropertyList([object class], &outCount);
    for (i = 0; iff0f0704fs
 * @param propertyName:
    userID
 * @return NSString
    ff0f0704
 */
+(NSString*)setXMLProperty:(NSString*)value propertyName:(NSString*)propertyName {
    
    NSString *retVal = @"";
    NSString *patternString = [NSString stringWithFormat:@"(?<=<%@>)(.*)(?=)",propertyName,propertyName];
    // CaseInsensitive:不区分大小写比较
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:patternString options:NSRegularExpressionCaseInsensitive error:nil];
    if (regex) {
        NSTextCheckingResult *firstMatch = [regex firstMatchInString:value options:NSCaseInsensitiveSearch range:NSMakeRange(0, [value length])];
        if (firstMatch) {
            retVal = [value substringWithRange:firstMatch.range];
        }
    }
    return retVal;
}

#pragma mark -
#pragma mark - 将标准的Json(实体)转换成NSMutableArray (List)
/**
 * 将标准的Json(实体)转换成NSMutableArray
 * @param xml:
     [{"UserID":"ff0f0704","UserName":"fs"},
      {"UserID":"ff0f0704","UserName":"fs"},...]
 * @param class:
    User
 * @return NSMutableArray (List)
 */
+(NSMutableArray*)jsonToArray:(NSString*)json class:(Class)class {
    
    NSMutableArray *retVal = [[[NSMutableArray alloc] init] autorelease];
    NSString *patternString = @"\\{.*?\\}";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:patternString options:0 error:nil];
    if (regex) {
        NSArray *match = [regex matchesInString:json options:0 range:NSMakeRange(0, [json length])];
        if (match) {
            for (NSTextCheckingResult *result in match) {
                NSString *jsonRow = [json substringWithRange:result.range];
                id object = [[class alloc] init];
                object = [self initWithJsonString:jsonRow object:object];
                [retVal addObject:object];
                [object release];
            }
        }
    }
    return retVal;
}

/**
 * 将传递过来的实体赋值
 * @param xml(忽略实体大小写差异):
    {"UserID":"ff0f0704","UserName":"fs"}
 * @param class:
    User @property userName,userID;
 * @return class
 */
+(id)initWithJsonString:(NSString*)json object:(id)object{
    
    unsigned int outCount, i;
    objc_property_t *properties = class_copyPropertyList([object class], &outCount);
    for (i = 0; i

Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<