Jak do UITableView dodać JSON URL?

0

Witam.
Nie wiem jak odwołać się do pliku JSON, aby wyświetlił się na symulatorze w TableView. Wyświetla mi się poprawnie plik w konsoli, Poniżej znajduje się kod, który napisałem:

#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];
   
}

- (void)didReceiveMemoryWarning {
   [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

   
   return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
   return 5;
}


- (UITableViewCell ​*)tableView:(UITableView *​)tableView cellForRowAtIndexPath:(NSIndexPath *)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);
   
  // for(NSDictionary *review in )

   
//    [self.tableView reloadData];
//    for (NSMutableDictionary *dic in result)
//    {
//        NSString *string = dic[@"array"];
//        if (string)
//        {
//            NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
//            dic[@"array"] = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//        }
//        else
//        {
//            NSLog(@"Error in url response");
//        }
//    }


}

























@end
1

nie do końca rozumiem o który element pracy z jsonem pytasz, ale mniej więcej wygląda to tak:
Masz ten array result i załóżmy, że parsowałeś takiego jsona:

 
{
    "x" : [
    {
        "id" : "1",
        "name" : "xx"
    },
    {
        "id" : "2",
        "name" : "xy"
    }
    ]
}

to możesz zrobić coś takiego:

 
for(id item in [result objectForKey:@"x"]){
   //tu mozesz np. wypisac sobie jakies pierdoly dla kazdego x
   NSLog(@"id: %@", [item objectForKey:@"id"]);
   NSLog(@"name: %@", [item objectForKey:@"name"]);
}
1

a jak chcesz potem te wartości użyć w komórce to daj property tam na górze w interface result i potem przypisz ten Twój result do self.result i w metodzie:
(UITableViewCell ​*)tableView:(UITableView *​)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

musisz jakoś zainicjować tą komórkę, np. w jej kontrolerze zrób metodę setupWithSmth(Smth *)smth, w niej przypiszesz co chcesz a w tej wyżej napisanej metodzie dasz wtedy [cell setupWithSmth:[self.result[indexPath.row]]

1 użytkowników online, w tym zalogowanych: 0, gości: 1