首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSON解析UITableViewController

JSON解析UITableViewController
EN

Stack Overflow用户
提问于 2014-03-31 07:25:21
回答 1查看 138关注 0票数 2

我正在尝试将我的JSON数据传递到UITableViewController。我成功地将我的一个JSON传递到UITableView中,并在下一个ViewController上显示它。问题是,我有4个JSON数据,我希望它能显示在ViewController上。由于JSON的格式,我无法执行ObjectForKey。

这是我的JSON

代码语言:javascript
复制
{
    uid: "60",
    name: "pae1344",
    mail: "viper1344@gmail.com",
    theme: "",
    signature: "",
    signature_format: "plain_text",
    created: "1396189622",
    access: "0",
    login: "1396189622",
    status: "1",
    timezone: "Asia/Bangkok",
    language: "",
    picture: "0",
    init: "viper1344@gmail.com",
    data: null,
    uri: "http://localhost/drupal/rest/user/60"
},

这是来自myTableViewController的代码。

代码语言:javascript
复制
#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显示什么了吗?

EN

回答 1

Stack Overflow用户

发布于 2014-03-31 07:30:09

您可以直接存储对NSDictionary的JSON响应。而不只是检查字典的打印结果。您将能够按键访问这些值。

下面是一个例子:

代码语言:javascript
复制
NSDictionary *myDic = // JSON response;
NSLog(@"User Id is : %@",[myDic valueForKey:@"uid"]);

希望这能帮到你。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22755762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档