Json&xml analysis and turn it into a model

Source: Internet
Author: User

//

Rootviewcontroller.m

JSON demo

//

Created by Liman on 14-6-20.

//Copyright (c) year Liman. All rights reserved.

//

#import "RootViewController.h"

#import "JSONManager.h"

#import "SHXMLParser.h"

#import "Model.h"

#import "XMLDictionary.h"

@interface rootviewcontroller ()

@end

@implementation Rootviewcontroller

#pragma mark- Tool Methods

// Delete the null key in the dictionary, {} ( only for use with parser shxmlparser)

-(ID) formatwithdict: (ID) dict

{

//1. parameter is not a dictionary , It is returned directly

if (![ Dict iskindofclass: [nsdictionaryclass]]) {

return dict;

}

//2. parameter is a dictionary , then further processing

nsmutabledictionary *mudict = [ nsmutabledictionary Dictionarywithdictionary:d ICT];

for (nsstring *key in [dict /c28>allKeys]) {

if ([[Dict objectforkey: Key] iskindofclass : [ nsdictionary class]]) {

if ( [[[Dict objectforkey: Key] allKeys] /c9>count] = = 0) {

// null dictionary processing

[mudict removeobjectforkey: key];

}

}

}

return mudict;

}

-(void) viewdidload

{

[Superviewdidload];

#warning-nsdata---> JSON dictionary /json strings

NSData *jsondata;

/ ///JSON String

NSString *jsonstr = [[NSString alloc] Initwithdata:jsondata encoding:nsutf8stringencoding];

/ ///JSON Dictionary

Nsdictionary *jsonstrdic = [[Jsonmanager alloc] transformdatatodic:jsondata];

#warning-json dictionary ---> Model

Nsdictionary *json = @{

@ "id": @ "10",

@ "Country": @ "Germany",

@ "Dialcode": @49,

@ "Isineurope": @YES

//                            };

Model *model = [[Model alloc] Initwithdictionary:json Error:nil];

#warning-json string ---> Model

NSString *path = [[NSBundle mainbundle] pathforresource:@ "EG" oftype:@ "json"];

NSString *json = [NSString stringwithcontentsoffile:path encoding:nsutf8stringencoding Error:nil];

Model *model = [[Model alloc] Initwithstring:json Error:nil];

// ------------------------------------  above : JSON, The following : XML--------------------------------------

#warning-nsdata---> XML dictionary ( two parser : the first empty dictionary bug appears , i.e.   {}; The second does not have an empty dictionary bug, the empty dictionary is removed directly )

NSData *xmldata;

//

//    // First Parser

Shxmlparser *parser = [[Shxmlparser alloc] init];

Nsdictionary *xmldic = [parser parsedata:xmldata];

//

//    // a second parser

Xmldictionaryparser *parser2 = [Xmldictionaryparser sharedinstance];

Nsdictionary *xmldic2 = [Parser2 dictionarywithdata:xmldata];

#warning-xml dictionary ---> Model

Nsdictionary *xmldic = @{

@ "id": @{},

@ "Country": @ "Germany",

@ "Dialcode": @49,

@ "Isineurope": @{},

//                            };

//

//    // Delete the null key in the dictionary, {} ( only for use with parser shxmlparser)

Nsdictionary *xmldic2 = [self formatwithdict:xmldic];

//

Model *model = [[Model alloc] Initwithdictionary:xmldic2 Error:nil];

}

@end

Json&xml analysis and turn it into a model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.