简体   繁体   中英

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.

The feed looks like this, in my code i'm trying to pull out all the titles. I dont know how to get down into the 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; and NSMutableData *data.

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.

Initially, the JSON is in the form of a dictionary (hence the curly braces in the image you provided). Therefore, you should adjust your initial parsing of the JSON to something like so:

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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