我的SBJson来生成和解析JSON串的代码

前端之家收集整理的这篇文章主要介绍了我的SBJson来生成和解析JSON串的代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、OC里生成指定格式JSON串的关键秘诀为:

①、每个{} 对应一个 NSDictionary

②、每个[] 对应一个 NSArray


二、代码如下:

#import @H_404_15@"ViewController.h"@H_404_15@

#import @H_404_15@"SBJson.h"@H_404_15@

@H_404_15@

@interface@H_404_15@ @H_404_15@ViewController@H_404_15@ ()@H_404_15@

{@H_404_15@

@H_404_15@NSString@H_404_15@ *strMyJson;@H_404_15@

}@H_404_15@

@H_404_15@

@end@H_404_15@

@H_404_15@

@implementation@H_404_15@ ViewController@H_404_15@

@H_404_15@

- (@H_404_15@void@H_404_15@)viewDidLoad {@H_404_15@

[@H_404_15@super@H_404_15@ @H_404_15@viewDidLoad@H_404_15@];@H_404_15@

@H_404_15@// Do any additional setup after loading the view,typically from a nib.@H_404_15@

@H_404_15@//@H_404_15@生成一个@H_404_15@JSON@H_404_15@串@H_404_15@

@H_404_15@if@H_404_15@ (![@H_404_15@self@H_404_15@ @H_404_15@writeJson@H_404_15@])@H_404_15@

{@H_404_15@

@H_404_15@NSLog@H_404_15@(@H_404_15@@"@H_404_15@写出@H_404_15@JSON@H_404_15@字符串失败@H_404_15@"@H_404_15@);@H_404_15@

@H_404_15@return@H_404_15@;@H_404_15@

}@H_404_15@

@H_404_15@else@H_404_15@

@H_404_15@NSLog@H_404_15@(@H_404_15@@"@H_404_15@写出@H_404_15@JSON@H_404_15@字符串成功@H_404_15@"@H_404_15@);@H_404_15@

@H_404_15@//@H_404_15@解析一个@H_404_15@JSON@H_404_15@串@H_404_15@

@H_404_15@if@H_404_15@ (![@H_404_15@self@H_404_15@ @H_404_15@readJson@H_404_15@])@H_404_15@

{@H_404_15@

@H_404_15@NSLog@H_404_15@(@H_404_15@@"@H_404_15@解析@H_404_15@JSON@H_404_15@字符串失败@H_404_15@"@H_404_15@);@H_404_15@

@H_404_15@return@H_404_15@;@H_404_15@

}@H_404_15@

@H_404_15@else@H_404_15@

@H_404_15@NSLog@H_404_15@(@H_404_15@@"@H_404_15@解析@H_404_15@JSON@H_404_15@字符串成功@H_404_15@"@H_404_15@);@H_404_15@

@H_404_15@

@H_404_15@

@H_404_15@

@H_404_15@

@H_404_15@

}@H_404_15@

@H_404_15@

-(@H_404_15@BOOL@H_404_15@) readJson@H_404_15@

{@H_404_15@

@H_404_15@@try@H_404_15@

{@H_404_15@

@H_404_15@SBJsonParser@H_404_15@ *parser=[[@H_404_15@SBJsonParser@H_404_15@ @H_404_15@alloc@H_404_15@] @H_404_15@init@H_404_15@];@H_404_15@

@H_404_15@NSDictionary@H_404_15@ *dic3_1=[parser @H_404_15@objectWithString@H_404_15@:@H_404_15@strMyJson@H_404_15@];@H_404_15@

@H_404_15@if@H_404_15@ (@H_404_15@nil@H_404_15@!=dic3_1)@H_404_15@

{@H_404_15@

@H_404_15@NSLog@H_404_15@(@H_404_15@@"%@"@H_404_15@,dic3_1.@H_404_15@allKeys@H_404_15@[@H_404_15@0@H_404_15@]);@H_404_15@

@H_404_15@NSDictionary@H_404_15@ *dic2_1=dic3_1.@H_404_15@allValues@H_404_15@[@H_404_15@0@H_404_15@];@H_404_15@

@H_404_15@NSLog@H_404_15@(@H_404_15@@"%@"@H_404_15@,dic2_1.@H_404_15@allKeys@H_404_15@[@H_404_15@0@H_404_15@]);@H_404_15@

@H_404_15@NSLog@H_404_15@(@H_404_15@@"%@"@H_404_15@,dic2_1.@H_404_15@allKeys@H_404_15@[@H_404_15@1@H_404_15@]);@H_404_15@

@H_404_15@NSDictionary@H_404_15@ *dic1=dic2_1.@H_404_15@allValues@H_404_15@[@H_404_15@0@H_404_15@];@H_404_15@

@H_404_15@NSDictionary@H_404_15@ *dic2=dic2_1.@H_404_15@allValues@H_404_15@[@H_404_15@1@H_404_15@];@H_404_15@

@H_404_15@NSLog@H_404_15@(@H_404_15@@"%@"@H_404_15@,dic1.@H_404_15@allKeys@H_404_15@[@H_404_15@0@H_404_15@]);@H_404_15@

@H_404_15@NSLog@H_404_15@(@H_404_15@@"%@"@H_404_15@,dic1.@H_404_15@allValues@H_404_15@[@H_404_15@0@H_404_15@]);@H_404_15@

@H_404_15@NSLog@H_404_15@(@H_404_15@@"%@"@H_404_15@,dic1.@H_404_15@allKeys@H_404_15@[@H_404_15@1@H_404_15@]);@H_404_15@

@H_404_15@NSLog@H_404_15@(@H_404_15@@"%@"@H_404_15@,dic1.@H_404_15@allValues@H_404_15@[@H_404_15@1@H_404_15@]);@H_404_15@

@H_404_15@NSLog@H_404_15@(@H_404_15@@"%@"@H_404_15@,dic2.@H_404_15@allKeys@H_404_15@[@H_404_15@0@H_404_15@]);@H_404_15@

@H_404_15@NSLog@H_404_15@(@H_404_15@@"%@"@H_404_15@,dic2.@H_404_15@allValues@H_404_15@[@H_404_15@0@H_404_15@]);@H_404_15@

@H_404_15@NSLog@H_404_15@(@H_404_15@@"%@"@H_404_15@,dic2.@H_404_15@allKeys@H_404_15@[@H_404_15@1@H_404_15@]);@H_404_15@

@H_404_15@NSLog@H_404_15@(@H_404_15@@"%@"@H_404_15@,dic2.@H_404_15@allValues@H_404_15@[@H_404_15@1@H_404_15@]);@H_404_15@

@H_404_15@return@H_404_15@ @H_404_15@YES@H_404_15@;@H_404_15@

}@H_404_15@

@H_404_15@else@H_404_15@

{@H_404_15@

@H_404_15@return@H_404_15@ @H_404_15@NO@H_404_15@;@H_404_15@

}@H_404_15@

}@H_404_15@

@H_404_15@@catch@H_404_15@ (NSException *exception)@H_404_15@

{@H_404_15@

@H_404_15@return@H_404_15@ @H_404_15@NO@H_404_15@;@H_404_15@

}@H_404_15@

}@H_404_15@

@H_404_15@

-(@H_404_15@BOOL@H_404_15@) writeJson@H_404_15@

{@H_404_15@

@H_404_15@@try@H_404_15@

{@H_404_15@

@H_404_15@NSDictionary@H_404_15@ *dic1=[@H_404_15@NSDictionary@H_404_15@ @H_404_15@dictionaryWithObjectsAndKeys@H_404_15@:@H_404_15@@"8888"@H_404_15@,@H_404_15@@"age"@H_404_15@,@H_404_15@@"174"@H_404_15@,@H_404_15@@"length"@H_404_15@,@H_404_15@nil@H_404_15@];@H_404_15@

@H_404_15@NSDictionary@H_404_15@ *dic2=[@H_404_15@NSDictionary@H_404_15@ @H_404_15@dictionaryWithObjectsAndKeys@H_404_15@:@H_404_15@@"9999"@H_404_15@,@H_404_15@@"178"@H_404_15@,@H_404_15@nil@H_404_15@];@H_404_15@

@H_404_15@NSDictionary@H_404_15@ *dic2_1=[@H_404_15@NSDictionary@H_404_15@ @H_404_15@dictionaryWithObjectsAndKeys@H_404_15@:dic1,@H_404_15@@"liji"@H_404_15@,dic2,@H_404_15@@"lixi"@H_404_15@,@H_404_15@nil@H_404_15@];@H_404_15@

@H_404_15@NSDictionary@H_404_15@ *dic3_1=[@H_404_15@NSDictionary@H_404_15@ @H_404_15@dictionaryWithObjectsAndKeys@H_404_15@:dic2_1,@H_404_15@@"person"@H_404_15@,@H_404_15@nil@H_404_15@];@H_404_15@

@H_404_15@SBJsonWriter@H_404_15@ *writer=[[@H_404_15@SBJsonWriter@H_404_15@ @H_404_15@alloc@H_404_15@] @H_404_15@init@H_404_15@];@H_404_15@

@H_404_15@NSString@H_404_15@ *strJson=[writer @H_404_15@stringWithObject@H_404_15@:dic3_1];@H_404_15@

@H_404_15@if@H_404_15@ (@H_404_15@nil@H_404_15@!=strJson)@H_404_15@

{@H_404_15@

@H_404_15@strMyJson@H_404_15@=strJson;@H_404_15@

@H_404_15@NSLog@H_404_15@(@H_404_15@@"%@"@H_404_15@,strJson);@H_404_15@

@H_404_15@return@H_404_15@ @H_404_15@YES@H_404_15@;@H_404_15@

}@H_404_15@

@H_404_15@else@H_404_15@

@H_404_15@return@H_404_15@ @H_404_15@NO@H_404_15@;@H_404_15@

@H_404_15@

}@H_404_15@

@H_404_15@@catch@H_404_15@(NSException *e)@H_404_15@

{@H_404_15@

@H_404_15@return@H_404_15@ @H_404_15@NO@H_404_15@;@H_404_15@

}@H_404_15@

}@H_404_15@

@H_404_15@

@H_404_15@

@H_404_15@

- (@H_404_15@void@H_404_15@)didReceiveMemoryWarning {@H_404_15@

[@H_404_15@super@H_404_15@ @H_404_15@didReceiveMemoryWarning@H_404_15@];@H_404_15@

@H_404_15@// Dispose of any resources that can be recreated.@H_404_15@

}@H_404_15@

@H_404_15@

@end@H_404_15@

猜你在找的Json相关文章