简体   繁体   English

Json Iphone解析数据

[英]Json Iphone Parse Data

I have tried this a few times and I still don't get how to go into a JSON feed and retrieve what I need to grab. 我已经尝试了几次,但仍然没有得到如何进入JSON提要并检索我需要抓取的内容。

The feed looks like this, in my code i'm trying to pull out all the titles. Feed看起来像这样,在我的代码中我试图取出所有的标题。 I dont know how to get down into the 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;
    }

and In my .H i have NSArray *news; 在我的.H我有NSArray *新闻; and NSMutableData *data. 和NSMutableData *数据。

Any help would be great, could you please explain your self clearly as I'm a total newbie to this language. 任何帮助都会很棒,请你清楚解释一下你的自我,因为我是这种语言的全新手。

Looking at the logic that you have in your code and the sample JSON that you posted, it doesn't look like your array is being populated with what you would want. 查看您在代码中使用的逻辑以及您发布的示例JSON,看起来您的数组似乎没有填充您想要的内容。

Initially, the JSON is in the form of a dictionary (hence the curly braces in the image you provided). 最初,JSON采用字典的形式(因此您提供的图像中有花括号)。 Therefore, you should adjust your initial parsing of the JSON to something like so: 因此,您应该将JSON的初始解析调整为如下所示:

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

From here, you can receive a key from the dictionary. 从这里,您可以从字典中接收密钥。 The one you'd be looking for is "array", which also actually is a dictionary despite its name. 你要找的那个是“数组”,尽管它的名字实际上也是一本字典。

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

Moving right along, you can finally access the "resources" array that you are looking for, and you can store that within the instance variable that you created in your .h file. 向右移动,您最终可以访问您正在查找的“资源”数组,并且可以将其存储在您在.h文件中创建的实例变量中。

news = arrayDictionary[@"resources"];

Now, in the cellForRowAtIndexPath method you can access various elements of this array based on the index path row that is provided to you. 现在,在cellForRowAtIndexPath方法中,您可以根据提供给您的索引路径行访问此数组的各种元素。

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

Finally, you can access various properties like the titles from that news item, and set the text label's text. 最后,您可以访问各种属性,例如该新闻项目的标题,并设置文本标签的文本。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM