首页 > 代码库 > IOS开发专题---转化解析JSON格式为字符格式(原生类库解析)
IOS开发专题---转化解析JSON格式为字符格式(原生类库解析)
1,不浪费时间,本代码经实践检验通过!
//网络请求代码段
//post request
NSURL *url=[NSURLURLWithString:@"http://192.168.253.101:8080/ERP/mobile/login.action"];
NSMutableURLRequest *request =[[NSMutableURLRequestalloc]initWithURL:urlcachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:10];
//设置请求方式
[requestsetHTTPMethod:@"POST"];
//设置参数
NSString *str=@"username=A023&password=1&master=UAS";
NSData *data=http://www.mamicode.com/[str>dataUsingEncoding:NSUTF8StringEncoding];
[requestsetHTTPBody:data];
//连接服务器
NSData *received=[NSURLConnectionsendSynchronousRequest:request returningResponse:nilerror:nil];
NSString *strl=[[NSStringalloc]initWithData:receivedencoding:NSUTF8StringEncoding];
// NSLog(@"结果:%@",strl); ---自定义json 格式字符串 包含集合和数组形式 采用原生类库解析的方式
NSError *error;
NSString *temstr=@"{\"count\":50,\"allprocess\":[{\"name\":\"神圣天使\"},{\"name\":\"商品天使\"},{\"name\":\"美丽天使\"}]}";
NSLog(@"temstr=%@",temstr);
NSData *rdata=http://www.mamicode.com/[temstr>dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"rdata=http://www.mamicode.com/%@",rdata);
NSDictionary *json=[temstrobjectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
NSLog(@"json=%@",json);
NSArray *elist=[json objectForKey:@"allprocess"];
NSString *uu=[elist objectAtIndex:1];
NSString *u_name=[[elist objectAtIndex:1] objectForKey:@"name"];
NSLog(@"elist=%@",elist);
NSLog(@"elist=%@",uu);
NSLog(@"elist=%@",u_name);
for (int i=0;i<[elistcount];i++) {
NSLog(@"遍历数组 \n %i=%@",i,[[elistobjectAtIndex:i] objectForKey:@"name"]);
}
IOS开发专题---转化解析JSON格式为字符格式(原生类库解析)