2013-02-27 90 views
0

我想分析中的iOS 5与AFNetworking框架服务器接收到的JSON数据:解析JSON在iOS 5中与AFNetworking

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
} failure:nil]; 

JSON数据我想分析这样的容貌:

[ { 
    addressReal = "\U6912\U6c5f\U89e3\U653e\U5357\U8def295\U53f7"; 
    areaTotal = 774; 
    balance = 0; 
    businessIncome = "556.49"; 
    equNoString = ""; 
    equStatusSting = ""; 
    id = 208; 
    staff = 10; 

}, 
    { 
    addressReal = "\U53f0\U5dde\U5e02\U5e9c\U5927\U9053668\U53f7"; 
    areaTotal = 156; 
    balance = 6463; 
    businessIncome = "174.93"; 
    equStatusSting = ""; 
    id = 209; 
    staff = 8; 

}] 

如何我现在可以访问JSON?

+0

我试图清理你的问题,使之能回答的,但我建议您尝试,以确保未来的问题,你总是清楚说明什么问题是你需要帮助。另请参见[如何提问智能路(http://catb.org/esr/faqs/smart-questions.html)(特别是:*准确和翔实的关于您的问题*) – unthought 2013-02-27 09:25:32

回答

0

这是我所用,随意它适合你的需求:

-(void) loginWithUserID:(NSString*)usrID User:(NSString*)usr Password:(NSString *)psw Sender:(id)sender 
{ 
    if ([sender respondsToSelector:@selector(startLoading)]){ 
     [sender performSelector:@selector(startLoading)]; 
    } 

    baseURL = [NSString stringWithFormat: 
        @"http://xxx.xxx.xxx.xxx/test/service.svc/weblogin"]; 
    NSString* serviceURL = [NSString stringWithFormat: 
          @"%@?userID=%@&user=%@&password=%@",baseURL,usrID,usr,psw; 

    NSURL *url = [NSURL URLWithString:[serviceURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; 

    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
     self.LoginOK = [JSON valueForKeyPath:@"LoginOK"]; 
     if (LoginOK.intValue == 1) { 
      NSDictionary *usrData = [JSON valueForKeyPath:@"userData"]; 
      userData = [[UserData alloc]init]; 
      [userData readFromJSONDictionary:usrData]; 
      NSLog(@"Userdata: %@",userData); 
      if ([sender respondsToSelector:@selector(loginSuccessful:)]) 
      { 
       [sender performSelector:@selector(loginSuccessful:)]; 
      } 
     }else{ 
      UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Error" message:@"No Correct Login Credentials" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
      [alertView show]; 
      ;} 

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
     if ([sender respondsToSelector:@selector(stopLoading)]){ 
      [sender performSelector:@selector(stopLoading)]; 
     } 
     [[[UIAlertView alloc] initWithTitle:@"Error" 
            message:@"Loginservice not available! Check your connection!" 
            delegate:nil 
          cancelButtonTitle:@"OK" 
          otherButtonTitles: nil] show]; 
     NSLog(@"Error: %@", error); 

    }]; 

    [operation start]; 
    }; 

,并使用

-(void)readFromJSONDictionary:(NSDictionary *)d 
{ 

    [self setProperty1:[d objectForKey:@"property1"]]; 
} 

方法来获得所有属性指出,词典添加到自定义类。

所以你的情况这将是使用

NSDictionary *data = [JSON valueForKeyPath:@"data"]; 
+0

[josn allobjects] – user1694024 2013-02-28 01:10:42

+0

嗯,是的,这是在情况下,JSON来到一个称为数据容器的例子。如果这个答案对你有帮助,并且你认为这是你问题的答案,请你点点旁边的复选标记以表达你的意见。如果它没有回答你的问题,或者如果你需要更多的澄清,请不要犹豫告诉! – 2013-03-08 11:55:32