2013-11-09 1 views
-2

이미지 링크가 포함 된 json 파일을 구문 분석하려고합니다. 이건 내 코드입니다 :iOS로 JSON URL 사진을 구문 분석 할 수 없습니다.

#import "Pictures.h" 
#import "DEMONavigationController.h" 
#import "PicturesObject.h" 

@interface Pictures() 
{ 
    NSInteger refreshIndex; 
    NSArray *images; 
    NSMutableArray *jsonIs; 
    NSArray *items; 
    IBOutlet UIImageView *imagesinsta; 
} 
@end 

@implementation Pictures 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.title = @"Pictures"; 
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Menu" 
                      style:UIBarButtonItemStylePlain 
                      target:(DEMONavigationController *)self.navigationController 
                      action:@selector(showMenu)]; 

    // ***************** FETCHING DATA ******************* // 
    NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"link-to-json.php?image=Image"]]; 
    NSData *data= [NSData dataWithContentsOfURL:URL]; 
    if (data == nil) { 
     return; 
    } 
    NSError* error; 
    items = [NSJSONSerialization JSONObjectWithData:data 
              options:kNilOptions 
               error:&error]; 

    NSLog(@"Json : %@",jsonIs); 

    if (jsonIs != nil) { 
     NSMutableDictionary* aDict = jsonIs[0]; 
     NSString *item_media = [aDict objectForKey:@"link"]; 
    } 

    // ***************** FETCHING DATA ******************* // 
} 

#pragma mark - Table view data source 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"PicturesObject"; 

    PicturesObject *cell = (PicturesObject *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PicturesObject" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 

    // The element in the array is going to be a dictionary. I JUST KNOW THIS. The key for the tweet is "text". 
    NSDictionary *item = [items objectAtIndex:indexPath.row]; 
    NSString *item_media = [item objectForKey:@"link"]; 

    return cell; 
} 

- (void)issueLoadRequest 
{ 
    // Dispatch this block asynchronosly. The block gets JSON data from the specified URL and performs the proper selector when done. 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"link-to-json.php?image=Image"]]; 
     [self performSelectorOnMainThread:@selector(receiveData:) withObject:data waitUntilDone:YES]; 
    }); 
} 

- (void)receiveData:(NSData *)data { 
    // When we have the data, we serialize it into native cocoa objects. (The outermost element from twitter is 
    // going to be an array. I JUST KNOW THIS. Reload the tableview once we have the data. 
    self.tweets = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 
    [self.myTableView reloadData]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return self.tweets.count; 
} 

@end 

이 어떻게 같은 내 사용자 지정 PicturesObject 테이블보기 모양입니다 :

:

PicturesObject

하지만 내 응용 프로그램을 실행할 때 나는 단지이 같은 화면을 얻을 Table View

+0

JSON 응답을 붙여 넣을 수 있습니까? – SRI

+0

NSLog에서 나는 다음을 얻습니다. Json : (null) @Ernaidu – tracifycray

+0

아니요. 데이터 변수를 가져 오는 데이터에 대해 묻고 있습니다. NSString을 NSString으로 변경하려고 시도하십시오. – SRI

답변

0

Json 데이터는 사전과 유사하며 배열이 아닙니다. 대신 사전에서 관련 데이터를 추출 후

NSDictionary items

NSArray items

사용을 사용하여의.

+0

좋아, 해결책 같아. 코드를 도와 주시겠습니까? @ vaibhav_15 – tracifycray

+0

jsonDict = [NSJSONSerialization JSONObjectWithData : 데이터 옵션 : NSJSONReadingMutableContainers 오류 : nil]; NSArray * items = [jsonDict objectForKey : @ "Links"]; –

+0

감사합니다. 그리고이 코드는 viewDidLoad 메서드에 있어야합니까? @ vaibhav_15 – tracifycray

관련 문제