簡體   English   中英

解析JSON數據以在iPhone的UItableview上列出

[英]Parsing JSON data to list on a UItableview for iPhone

我確實嘗試將JSON數據帶到iPhone並使用NSLOG連接打印傳入的JSON數據。能夠打印和測試特定節點。我開始使用for循環遍歷feed數組並將其顯示在UItableview卻無法做到。 嘗試了多種選擇,但無法弄清這個選擇。請看一下代碼,讓我知道我在哪里做錯了。我仍在學習iPhone編碼,因此任何inout都將非常有幫助。請找到代碼下面。

#import "RootViewController.h"
#import "SBJson.h"
#import "JSON.h"

@implementation RootViewController

NSMutableArray *listOfStates;
NSArray *exercises;
NSMutableData *responseData;


- (void)viewDidLoad
{
    //---initialize the array---
    listOfStates = [[NSMutableArray alloc] init];


    responseData = [[NSMutableData data] retain];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost/~xxxx/MyWorks/index.php?playerid=0"]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self ];
    //NSURL *url = [NSURL URLWithString:@""]; 

    [super viewDidLoad];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSLog(@"didReceiveResponse"); [responseData setLength:0]; }
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data]; }

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Connection failed: %@", [error description]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    [responseData release];

    NSDictionary *dictionary = [responseString JSONValue];
    NSArray *response = [dictionary valueForKey:@"feed"];

    NSLog(@"Here is the title of the feed: %@", [response valueForKey:@"video"]);


    exercises = [[NSArray alloc] initWithArray:response];

    [[self tableView] reloadData];

}




/*
 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 */

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [exercises count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    [responseData release];

    NSDictionary *dictionary = [responseString JSONValue];
    NSArray *response = [dictionary valueForKey:@"feed"];

    //create a cell
    UITableViewCell *cell = [[UITableViewCell alloc]
                             initWithStyle:UITableViewCellStyleDefault
                             reuseIdentifier:@"cell"];



    // fill it with contnets

    int ndx;
    for (ndx = 0; ndx <dictionary.count; ndx++) {
        NSDictionary *player = (NSDictionary *)[dictionary objectAtIndex:ndx];
        NSLog(@"Player: %@", [player valueForKey:@"player"]); 
    }
    NSString *cellValue = [player objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;
    // return it
    return cell;

}

在不知道發生了什么情況的情況下很難說出問題所在,但是這里有一些觀察。

  • 看來您正在解析響應很多次。 在connectionDidFinishLoading中創建“ exercises”數組,然后每次通過tableView:cellForRowAtIndexPath:呈現單元時再次進行。 我假設您第一次從JSON數據創建演習數組時會獲得所需的所有數據。 在這種情況下,tableView:cellForRowAtIndexPath:應該只需要將對象從Exercises數組中取出,而不是處理響應數據即可。
  • 同樣在這里,我無法弄清楚您要在使用“ player”的循環中要做什么-好像您正在記錄信息,但不對那些player對象做任何事情。 除了不使用它們外,在我看來,這些未發布的對象還泄漏了內存。

根據您在注釋中添加的JSON,看來Exercises數組將包含NSDictionary對象。 這意味着您設置單元格文本的代碼不正確。 代替

NSString *cellValue = [response objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

你可能想要像

NSDictionary *exercise = [response objectAtIndex:indexPath.row];
cell.textLabel.text = [exercise valueForKey"player"];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM