iphone开发之iphone解析xml&json-2

解析json文件示例。

首先在项目中加入解析json的相关包。

1,JSON.h 

2,NSObject+SBJSON.h & NSObject+SBJSON.m

3,NSString+SBJSON.h & NSString+SBJSON.m

4,SBJSON.h & SBJSON.m

代码1

 

 1 #import <UIKit/UIKit.h>
2
3 @interface jsonTestAppDelegate : NSObject <UIApplicationDelegate> {
4 UIWindow *window;
5 }
6
7 @property (nonatomic, retain) IBOutlet UIWindow *window;
8 -(void)json1;
9 -(void)json2;
10 -(void)json3;
11 -(void)json4;
12 @end


代码2

 

  1 #import "jsonTestAppDelegate.h"
2 #import "SBJSON.h"
3 @implementation jsonTestAppDelegate
4
5 @synthesize window;
6 //{"recommend":[{ },{ },{ },{ },{ }]}
7 //{"recommend":"资讯机器人","dog":"狗仔机器人"};
8 NSString *str1 = @"{\"recommend\":\"资讯机器人\",\"dog\":\"狗仔机器人\"}";
9 NSString *str2=@"{\"data\":{\"user\":\"vcom\",\"userToken\":\"xx\"}}";
10 NSString *str3 =@"{\"data\":[{\"recommend\":\"资讯机器人\",\"dog\":\"狗仔机器人\"}],\"data2\":[{\"recommend\":\"资讯机器人\",\"dog\":\"狗仔机器人\"}] }";
11 NSString *str4 = @"{\"data\":[{\"recommend\":\"资讯机器人\",\"dog\":\"狗仔机器人\"},{\"recommend\":\"资讯机器人2\",\"dog\":\"狗仔机器人2\"}]}";
12 - (void)applicationDidFinishLaunching:(UIApplication *)application {
13
14 //[self json1];
15 //[self json2];
16 //[self json3];
17 [self json4];
18 [window makeKeyAndVisible];
19 }
20
21 //{"recommend":"资讯机器人","dog":"狗仔机器人"}
22 -(void)json1{
23 NSLog(@"要解析的字符串%@",str1);
24 //创建JSON对象
25 SBJSON *json = [[SBJSON new] autorelease];
26
27 NSDictionary *jsonDic = [json objectWithString:str1 error:nil];
28
29 NSString *s=[jsonDic objectForKey:@"recommend"];
30 NSString *s2=[jsonDic objectForKey:@"dog"];
31
32 NSLog(@"%@",s);
33 NSLog(@"%@",s2);
34 }
35
36 //{"data":{"user":"vcom","userToken":"xx"},"data2":{"user":"vcom","userToken":"xx"}}
37 -(void)json2{
38 NSLog(@"要解析的字符串%@",str2);
39 SBJSON *json = [[SBJSON new] autorelease];
40 NSDictionary *jsonDic = [json objectWithString:str2 error:nil];
41
42 NSDictionary *userlist=nil;
43 //遍历大字典中所有的键
44 for(NSString *key in jsonDic)
45 {
46 if([key compare:@"data"] == NSOrderedSame)
47 {
48 userlist=[jsonDic objectForKey:key];
49
50 NSString *s2=[userlist objectForKey:@"user"];
51 NSLog(@"======%@",s2);
52 }
53 }
54
55 }
56
57 //{"data":[{"recommend":"资讯机器人","dog":"狗仔机器人"}],"data2":[{"recommend":"资讯机器人","dog":"狗仔机器人"}] }
58 -(void)json3{
59 NSLog(@"要解析的字符串%@",str3);
60 SBJSON *json = [[SBJSON new] autorelease];
61 NSDictionary *jsonDic = [json objectWithString:str3 error:nil];
62 //NSArray *array=[jsonDic objectForKey:@"data"];
63 // NSDictionary *dict=[array objectAtIndex:0];
64 // NSString *rec=[dict objectForKey:@"recommend"];
65
66 NSMutableArray *userlist=nil;
67 for(NSString *key in jsonDic)
68 {
69 if([key isEqualToString:@"data2"])
70 {
71 userlist=[jsonDic objectForKey:key];
72 //遍历数组中的每一个字典
73 for(NSDictionary *dic in userlist){
74 NSString *s2=[dic objectForKey:@"dog"];
75 NSLog(@"******%@",s2);
76 }
77 }
78 }
79
80 }
81
82 //{"data":[{"recommend":"资讯机器人","dog":"狗仔机器人"},{"recommend":"资讯机器人2","dog":"狗仔机器人2"}]}
83 -(void)json4{
84 NSLog(@"要解析的字符串%@",str4);
85 SBJSON *json = [[SBJSON new] autorelease];
86 NSDictionary *jsonDic = [json objectWithString:str4 error:nil];
87
88 NSMutableArray *userlist;
89 for(NSString *key in jsonDic)
90 {
91 NSLog(@"key:%@",key);
92 if([key compare:@"data"] == NSOrderedSame)
93 {
94 userlist=[jsonDic objectForKey:key];
95 NSLog(@"------%@",userlist);
96 NSLog(@"长度:%D",[userlist count]);
97
98 for(NSDictionary *dic in userlist){
99
100 NSString *s2=[dic objectForKey:@"dog"];
101 NSLog(@"解析的内容:%@",s2);
102
103 }
104 }
105 }
106
107 }
108
109 - (void)dealloc {
110 [window release];
111 [super dealloc];
112 }
113
114
115 @end



你可能感兴趣的:(iPhone开发)