//
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