Investor06
Investor06

Reputation: 27

JSON parsing UITableViewController

I'm trying to pass my JSON Data into the UITableViewController. I did manage to pass one of my JSON into the UITableView and display it on the next ViewController when the user performsegue. The problem is that I got 4 of my JSON Data that I want it to this display on the ViewController. I can't do the ObjectForKey due to the format of my JSON.

Here is my 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"
},

Here is the code from the 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];
//    


}

Is there a way for me to make my JSON into a Dictionary so, I can select what to be shown by using ObjectForKey ?

Upvotes: 2

Views: 143

Answers (2)

prelite
prelite

Reputation: 1073

First of all use this library by adding .m and .h files to your project:

JSONKit

Then you can turn JSON strings into dictionary.

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

Upvotes: 2

You can directly store your JSON response to NSDictionary. Than just check the print results of that Dictionary. You will be able to access the values by key.

Here is example :

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

Hope this will help you.

Upvotes: 0

Related Questions