• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - JSON解析UITableViewController

[复制链接]
菜鸟教程小白 发表于 2022-12-12 09:05:52 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试将我的 JSON 数据传递到 UITableViewController。我确实设法将我的一个 JSON 传递到 UITableView 并在用户执行 egue 时将其显示在下一个 ViewController 上。问题是我得到了 4 个 JSON 数据,我希望它显示在 ViewController 上。由于我的 JSON 格式,我无法执行 ObjectForKey。

这是我的 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)initWithStyleUITableViewStyle)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)numberOfSectionsInTableViewUITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

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

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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)prepareForSegueUIStoryboardSegue *)segue senderid)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 选择要显示的内容?



Best Answer-推荐答案


首先通过将 .m 和 .h 文件添加到您的项目来使用此库:

JSONKit

然后您可以将 JSON 字符串转换为字典。

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

关于ios - JSON解析UITableViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22755762/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap