2

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 ?

1
  • The best way to parse dictionary is Create an NSObject class and parse from there, and make objects. Commented Mar 31, 2014 at 7:57

2 Answers 2

2

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"]);
Sign up to request clarification or add additional context in comments.

1 Comment

could you please accept my answer as the good one if you used my solution? thank you
0

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.

4 Comments

sry I just found another problem after changing from NSArray to NSDictionary for my JSON response.In the CellForRowIndexPath how should I implement that ? cell.txtEnterPriseName.text = [self.googlePlacesArrayFromAFNetworking objectAtIndex:indexPath.row]; Thank you.
You can pass like this : cell.txtEnterPriseName.text = [yourDicName valueForKey@"name"];
Thank you very much for all your help but...... I come across another problem again T__T It seems that by passing like that it will only pass 1 value which is in this example is name to the next viewController(Not on the UItableviewController). How should I implement this in such a way that I can just pass 1 JSON as a block/group then classifying the valueForKey to match up with each UILabel.text after the user performsegue? Sorry for asking too many question.
I got it working now! don't worry about it Thank you very much for all your help, I'm very appreciated God Bless you. Have a great day/night where ever you are.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.