2013-02-27 91 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