OGeek|极客世界-中国程序员成长平台

标题: ios - 如何将 JSON URL 响应数据加载到 tableView? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 13:00
标题: ios - 如何将 JSON URL 响应数据加载到 tableView?

我正在学习 iOS,在将 JSON URL 添加到 tableView 时遇到问题。如何调用 TableView?现在,仅在控制台中显示 JSON URL。请帮我。 不知道哪里出错了。

//  WordsViewController.m


#import "WordsViewController.h"
#import "Word.h"


@interface WordsViewController ()

@end

@implementation WordsViewController
@synthesize jsonArray, wordsArray;


- (void)viewDidLoad {
    [super viewDidLoad];
    //set the title of our VC
    self.title = @"SŁOWNIK";

    //LOAD DATA
    [self simpleJsonParsing];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableViewUITableView *)tableView {
    return 1;
}

- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section {
    return 5;
}


- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier"Cell" forIndexPath:indexPath];

    return cell;
}

- (void)simpleJsonParsing {
    //-- Make URL request with server
    NSHTTPURLResponse *response = nil;
    NSString *jsonUrlString = [NSString stringWithFormat"https://uidictionary.herokuapp.com/phrases.json"];
    NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    //-- Get request and response though URL
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

    //-- JSON Parsing
    NSMutableArray *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];

    NSData * data = [NSData dataWithContentsOfURL:url];

    NSLog(@"Result = %@",result);
}

@end

控制台显示:

2016-02-18 13:01:42.645 Słownik[20421:5341020] Result = {
    phrases =     (
        {
            expression = pejoratywny;
            meaning = "\U201eCo\U015b, co jest pejoratywne, wyra\U017ca czyj\U0105\U015b negatywn\U0105 ocen\U0119 lub, rzadziej, samo jest przedmiotem takiej oceny\U201d.\U00a0Inny s\U0142ownik j\U0119zyka polskiego\U00a0PWN, z kt\U00f3rego przytoczy\U0142em t\U0119 definicj\U0119, podaje gar\U015b\U0107 cytat\U00f3w, w nich za\U015b znajdziemy takie po\U0142\U0105czenia wyrazowe, jak\U00a0znaczenie pejoratywne,okre\U015blenie pejoratywne,\U00a0cechy pejoratywne. Jak st\U0105d wynika, przymiotnik ten ma do\U015b\U0107 szeroki zakres u\U017cycia. \U0141\U0105cz\U0105c go ze s\U0142owem\U00a0zabarwienie, nie dublujemy jego tre\U015bci.";
        },
        {
            expression = "Ruby on Rails";
            meaning = "\U00a0(cz\U0119sto nazywany\U00a0RoR\U00a0lub po prostu\U00a0Rails) \U2013\U00a0framework\U00a0open source\U00a0do szybkiego tworzenia\U00a0aplikacji webowych\U00a0stworzony g\U0142\U00f3wnie przez du\U0144skiego programist\U0119\U00a0Davida Heinemeiera Hanssona\U00a0w ramach pracy nad oprogramowaniem\U00a0Basecamp. RoR zosta\U0142 napisany w j\U0119zyku\U00a0Ruby\U00a0z u\U017cyciem architektury\U00a0MVC\U00a0(ang.\U00a0Model-View-Controller).";
        },
    );
}



Best Answer-推荐答案


喜欢

创建一个 NSMutableArray iyour viewdidLoad

@interface WordsViewController ()
{
 NSMutableArray *finalResultArray;
 }
 @end


- (void)viewDidLoad {
[super viewDidLoad];
//set the title of our VC
self.title = @"SŁOWNIK";

 finalResultArray = [NSMutableArray new];
//LOAD DATA
[self simpleJsonParsing];

}

//-- JSON Parsing
NSArray *result = [[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil]objectForKey"phrases"];

  [finalResultArray removeAllobjects];
NSLog(@"Result = %@",result);
 for (NSMutableDictionary *dic in result)
{


NSMutableDictionary *temp = [NSMutableDictionary new];
[temp setObject:[tmp objectForKey"expression"] forKey"expression"];
[temp setObject:[tmp objectForKey"meaning"] forKey"meaning"];

[finalResultArray addObject:temp];
 }

if (finalResultArray)
{
[self.tableView reloadData];
}


  - (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section {

    return finalResultArray.count;
}


- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier"Cell" forIndexPath:indexPath];

   cell.textlabel.text = [[finalResultArray objectAtIndex:indexPath.row] objectForKey"expression"];

    return cell;
}

关于ios - 如何将 JSON URL 响应数据加载到 tableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35481482/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4