2014-03-31 2 views
2

JSON 데이터를 UITableViewController에 전달하려고합니다. JSON 중 하나를 UITableView로 전달하고 사용자가 성능을 향상시킬 때 다음 ViewController에 표시했습니다. 문제는 ViewController에서이 디스플레이에 원하는 JSON 데이터 4 개가 있다는 것입니다. 내 JSON의 형식으로 인해 ObjectForKey를 수행 할 수 없습니다.JSON 파싱 UITableViewController

여기 여기 내 JSON

{ 
    uid: "60", 
    name: "pae1344", 
    mail: "[email protected]", 
    theme: "", 
    signature: "", 
    signature_format: "plain_text", 
    created: "1396189622", 
    access: "0", 
    login: "1396189622", 
    status: "1", 
    timezone: "Asia/Bangkok", 
    language: "", 
    picture: "0", 
    init: "[email protected]", 
    data: null, 
    uri: "http://localhost/drupal/rest/user/60" 
}, 

인 myTableViewController에서 코드입니다.

#import "TestinViewController.h" 
#import "AFNetworking.h" 
#import "TestinCell.h" 
#import "EDscriptionViewController.h" 
@interface TestinViewController() 

@end 

@implementation TestinViewController 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.finishedGooglePlacesArray = [[NSArray alloc] init]; 
    [self makeRestuarantsRequests]; 

    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
-(void)makeRestuarantsRequests 
{ 
    NSURL *url = [NSURL URLWithString:@"http://localhost/drupal/rest/enterpriselist.json"]; 

    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

    AFJSONRequestOperation *operation = [AFJSONRequestOperation 
             JSONRequestOperationWithRequest:request 
             success:^(NSURLRequest *request, NSHTTPURLResponse *response, id responseObject) 
             { 

              self.googlePlacesArrayFromAFNetworking = [responseObject valueForKey:@"node_title"]; 
              self.body = [responseObject valueForKey:@"Body"]; 
              self.tel = [responseObject valueForKey:@"Enterprise Tel"]; 
              self.mail = [responseObject valueForKey:@"Enterprise email"]; 

               [self.tableView reloadData]; 
              NSLog(@"%@", self.googlePlacesArrayFromAFNetworking); 
              NSLog(@"%@" , self.body); 
              NSLog(@"%@", self.tel); 
              NSLog(@"%@", self.mail); 
             } 
             failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id responseObject) 
             { 
              NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo); 
             }]; 

    [operation start]; 

} 
#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
#warning Potentially incomplete method implementation. 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
#warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    return [self.googlePlacesArrayFromAFNetworking count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    TestinCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell ==Nil){ 
     cell = [[TestinCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.txtEnterPriseName.text = [self.googlePlacesArrayFromAFNetworking objectAtIndex:indexPath.row]; 
    cell.txtEnterPriseBody.text = [self.body objectAtIndex:indexPath.row]; 
    cell.txtEnterPriseEmail.text = [self.mail objectAtIndex:indexPath.row]; 
    cell.txtEnterPriseTel.text = [self.tel objectAtIndex:indexPath.row]; 
    // Configure the cell... 

    return cell; 
} 
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 

     NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow]; 
     EDscriptionViewController *destViewController = (EDscriptionViewController*) segue.destinationViewController ; 
     //  destViewController.enterprise = [enterPrise_names objectAtIndex:indexPath.row]; 
     destViewController.detail = [self.googlePlacesArrayFromAFNetworking objectAtIndex:indexPath.row]; 


    destViewController.Ebody = [self.body objectAtIndex:indexPath.row]; 
    destViewController.EEmail = [self.mail objectAtIndex:indexPath.row]; 
    destViewController.ETel = [self.tel objectAtIndex:indexPath.row]; 
    // destViewController.ebody = [self.body objectAtIndex:indexPath.row]; 
// destViewController.etel = [self.tel objectAtIndex:indexPath.row]; 
// destViewController.email = [self.mail objectAtIndex:indexPath.row]; 
//  


} 

내 JSON을 사전으로 만들 수있는 방법이 있습니까? 그렇다면 ObjectForKey를 사용하여 표시 할 항목을 선택할 수 있습니까?

+0

사전을 구문 분석하는 가장 좋은 방법은 NSObject 클래스를 만들고 거기에서 구문 분석하고 개체를 만드는 것입니다. –

답변

2

먼저 프로젝트에하는 .m 및 .H 파일을 추가하여이 라이브러리를 사용 :

JSONKit

은 그럼 당신은 사전에 JSON 문자열을 설정할 수 있습니다.

//JSONString is a NSString variable with the JSON 
NSDictionary *myJSONDic = [JSONString objectFromJSONString] 
NSLog(@"User Id is : %@",[myJSONDic valueForKey:@"uid"]); 
+0

@prelite 주셔서 감사합니다! – user3027109

+0

내 솔루션을 사용했다면 내 대답을 좋은 것으로 받아 들일 수 있습니까? 고맙습니다 – prelite

0

NSDictionary에 JSON 응답을 직접 저장할 수 있습니다. 그 사전의 인쇄 결과를 확인하기 만하면됩니다. 키를 사용하여 값에 액세스 할 수 있습니다. 이 당신을 도울 것입니다

NSDictionary *myDic = // JSON response; 
NSLog(@"User Id is : %@",[myDic valueForKey:@"uid"]); 

희망 : 여기

은 예입니다. 모든

+0

대단히 감사합니다 .Mayur it works !!!!!!!! – user3027109

+0

@ user3027109 환영합니다 :) – Mayur

+0

sry 방금 NSArray에서 NSDictionary로 JSON 응답을 변경 한 후 다른 문제를 발견했습니다. CellForRowIndexPath에서 어떻게 구현해야합니까? cell.txtEnterPriseName.text = [self.googlePlacesArrayFromAFNetworking objectAtIndex : indexPath.row]; 감사합니다. – user3027109