我的SBJson来生成和解析JSON串的代码
来源:互联网 发布:mac 切换大小写设置 编辑:程序博客网 时间:2024/06/11 17:44
一、OC里生成指定格式JSON串的关键秘诀为:
①、每个{} 对应一个 NSDictionary
②、每个[] 对应一个 NSArray
二、代码如下:
#import "ViewController.h"
#import "SBJson.h"
@interface ViewController ()
{
NSString *strMyJson;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//生成一个JSON串
if (![selfwriteJson])
{
NSLog(@"写出JSON字符串失败");
return;
}
else
NSLog(@"写出JSON字符串成功");
//解析一个JSON串
if (![selfreadJson])
{
NSLog(@"解析JSON字符串失败");
return;
}
else
NSLog(@"解析JSON字符串成功");
}
-(BOOL) readJson
{
@try
{
SBJsonParser *parser=[[SBJsonParseralloc] init];
NSDictionary *dic3_1=[parserobjectWithString:strMyJson];
if (nil!=dic3_1)
{
NSLog(@"%@",dic3_1.allKeys[0]);
NSDictionary *dic2_1=dic3_1.allValues[0];
NSLog(@"%@",dic2_1.allKeys[0]);
NSLog(@"%@",dic2_1.allKeys[1]);
NSDictionary *dic1=dic2_1.allValues[0];
NSDictionary *dic2=dic2_1.allValues[1];
NSLog(@"%@",dic1.allKeys[0]);
NSLog(@"%@",dic1.allValues[0]);
NSLog(@"%@",dic1.allKeys[1]);
NSLog(@"%@",dic1.allValues[1]);
NSLog(@"%@",dic2.allKeys[0]);
NSLog(@"%@",dic2.allValues[0]);
NSLog(@"%@",dic2.allKeys[1]);
NSLog(@"%@",dic2.allValues[1]);
returnYES;
}
else
{
returnNO;
}
}
@catch (NSException *exception)
{
returnNO;
}
}
-(BOOL) writeJson
{
@try
{
NSDictionary *dic1=[NSDictionarydictionaryWithObjectsAndKeys:@"8888",@"age",@"174",@"length",nil];
NSDictionary *dic2=[NSDictionarydictionaryWithObjectsAndKeys:@"9999",@"age",@"178",@"length",nil];
NSDictionary *dic2_1=[NSDictionarydictionaryWithObjectsAndKeys:dic1,@"liji",dic2,@"lixi",nil];
NSDictionary *dic3_1=[NSDictionarydictionaryWithObjectsAndKeys:dic2_1,@"person",nil];
SBJsonWriter *writer=[[SBJsonWriteralloc] init];
NSString *strJson=[writerstringWithObject:dic3_1];
if (nil!=strJson)
{
strMyJson=strJson;
NSLog(@"%@",strJson);
returnYES;
}
else
returnNO;
}
@catch(NSException *e)
{
returnNO;
}
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
- 我的SBJson来生成和解析JSON串的代码
- sbjson数据的生成和解析
- SBJson数据的生成和解析
- xcode4.2.1 中使用 sbjson 的 3.1版本来解析json字符串
- sbjson的解析
- iOS 使用SBJSON创建和解析JSON
- iOS 使用SBJSON创建和解析JSON
- Json的解析和生成
- Json的解析和生成
- json的生成和解析
- 服务器返回的json数据,通过本地解析调用SBjson的类
- sbjson解析出的json字符串包含null时的处理方法
- ios 中使用SBJson拼接和解析json
- ios 中使用SBJson拼接和解析json
- iOS json解析之SBJson
- IOS中四种解析JSON的方法(IOS5自带支持方法、JSONKit、SBJson、TouchJson)
- JSON数据的生成和解析
- 使用JSONObject来生成JSON和解析JSON
- Android 获取服务器数据解析Text数据,存在htmel标签的处理办法
- iOS UITableView
- Objective-C NSFileManager 管理文件和目录
- C程序中丢弃输入行无用的代码
- iOS软件开发 Core Data的使用
- 我的SBJson来生成和解析JSON串的代码
- Gym 100685G Gadget Hackwrench (LCA)
- hdu5497 Inversion
- 求解集合A与B的差集
- Merge Two Sorted Lists and Merge k Sorted Lists
- 剑指offer—数组中的逆序对
- Android创建和删除文件夹和文件
- HDU 2064 汉诺塔III
- struts2笔记2