簡體   English   中英

Json Iphone解析數據

[英]Json Iphone Parse Data

我已經嘗試了幾次,但仍然沒有得到如何進入JSON提要並檢索我需要抓取的內容。

Feed看起來像這樣,在我的代碼中我試圖取出所有的標題。 我不知道如何進入Json Feed。

在此輸入圖像描述

    - (void)viewDidLoad
    {
        [super viewDidLoad];


        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

        NSURL *url = [NSURL URLWithString:@"http://api.storageroomapp.com/accounts/511a4f810f66026b640007b8/collections/511a51580f66023bff000ce9/entries.json?auth_token=Zty6nKsFyqpy7Yp5DP1L&preview_api=1"];

        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [[NSURLConnection alloc] initWithRequest:request delegate:self];



        // Do any additional setup after loading the view from its nib.
    }

    -(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
        data = [[NSMutableData alloc] init];
    }
    -(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData{
        [data appendData:theData];
    }
    -(void)connectionDidFinishLoading:(NSURLConnection *) connection{
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

        news = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
        [mainTableView reloadData];
    }

    -(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
        UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The data could not be downloaded - please make sure you're connected to either 3G or Wi-FI" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [errorView show];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    }


    -(int)numberOfSectionsInTableView:(UITableView *)tableView{
        return  1;
    }
    -(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return [news count];
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];


            cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"title"];

        }

        return cell;
    }

在我的.H我有NSArray *新聞; 和NSMutableData *數據。

任何幫助都會很棒,請你清楚解釋一下你的自我,因為我是這種語言的全新手。

查看您在代碼中使用的邏輯以及您發布的示例JSON,看起來您的數組似乎沒有填充您想要的內容。

最初,JSON采用字典的形式(因此您提供的圖像中有花括號)。 因此,您應該將JSON的初始解析調整為如下所示:

NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];

從這里,您可以從字典中接收密鑰。 你要找的那個是“數組”,盡管它的名字實際上也是一本字典。

NSDictionary *arrayDictionary = dictionary[@"array"];

向右移動,您最終可以訪問您正在查找的“資源”數組,並且可以將其存儲在您在.h文件中創建的實例變量中。

news = arrayDictionary[@"resources"];

現在,在cellForRowAtIndexPath方法中,您可以根據提供給您的索引路徑行訪問此數組的各種元素。

NSDictionary *newsItem = news[[indexPath row]];

最后,您可以訪問各種屬性,例如該新聞項目的標題,並設置文本標簽的文本。

NSString *title = newsItem[@"title"];
[[cell textLabel] setText:title];

暫無
暫無

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

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