2016-09-02 7 views
1

私はJSONを取得し、JSON辞書画像、名前、URLで取得します。しかし、私はJSON辞書にコメントを入れて、tableViewにリロードします。私はしようとしたが、null値を取得します。ですから、コメントを取得してtableviewで再読み込みする方法を手伝ってください。私のコードをチェックし、私が間違っている場所を教えてください。ありがとうございました。JSON Dictionaryを解決するには

MY JSON

(
    { 
    image = "http://sixthsenseit.com/school/project/uploades/160223032210.png"; 
    likes = 3; 
    name = "RADHIKA SAXENA"; 
    url = "http://sixthsenseit.com/school/my-school/uploads/photo_1448551101.jpg"; 
}, 
    { 
    comment =   (
        { 
      comment = q; 
      name = ""; 
     }, 
        { 
      comment = f; 
      name = ""; 
     }, 
        { 
      comment = ffgggggggg; 
      name = ""; 
     }, 
        { 
      comment = vcvg; 
      name = ""; 
     }, 
        { 
      comment = ggg; 
      name = ""; 
     }, 
        { 
      comment = aad; 
      name = ""; 
     }, 
        { 
      comment = anku; 
      name = ""; 
     }, 
        { 
      comment = gffgffg; 
      name = ""; 
     }, 
        { 
      comment = fggg; 
      name = ""; 
     }, 
        { 
      comment = vgghj; 
      name = ""; 
     }, 
        { 
      comment = ffgfh; 
      name = testing; 
     }, 
        { 
      comment = bnVib; 
      name = "HARSHALI SHARMA"; 
     } 
    ); 
} 
) 

のviewDidLoadのGet応答

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://sixthsenseit.com/school/project/ios/image_data.php"]]; 


//create the Method "GET" or "POST" 
[request setHTTPMethod:@"POST"]; 

//Pass The String to server(YOU SHOULD GIVE YOUR PARAMETERS INSTEAD OF MY PARAMETERS) 
NSString *userUpdate =[NSString stringWithFormat:@"pid=%@&uid=%@&",@"29",@"1000710017", nil]; 



//Check The Value what we passed 
// NSLog(@"the data Details is =%@", userUpdate); 

//Convert the String to Data 
NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding]; 

//Apply the data to the body 
[request setHTTPBody:data1]; 

//Create the response and Error 
NSError *err; 
NSURLResponse *response; 

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; 


NSArray *json = [NSJSONSerialization JSONObjectWithData:responseData 
                options:kNilOptions 
                 error:&err]; 

    NSLog(@"results == %@",json); 

_name.text = [[json objectAtIndex:0]objectForKey:@"name"]; 

image = [[json objectAtIndex:0]objectForKey:@"url"]; 
NSURL *url = [NSURL URLWithString:image]; 
NSData *imageData = [NSData dataWithContentsOfURL:url]; 
_image.image = [UIImage imageWithData:imageData]; 

url = [[json objectAtIndex:0]objectForKey:@"image"]; 
NSURL *urls = [NSURL URLWithString:url]; 
NSData *imageDatas = [NSData dataWithContentsOfURL:urls]; 
_url.image = [UIImage imageWithData:imageDatas]; 

comment_ary = [json valueForKey:@"comment"]; 

テーブルビューメソッド

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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
//NSLog(@"tableview cell"); 
CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"htrcell"]; 
if (cell==nil) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
} 
NSDictionary *getValues = comment_ary [indexPath.row]; 
cell.name.text=[NSString stringWithFormat:@"%@",getValues [@"name"]]; 
cell.comment.text=[NSString stringWithFormat:@"%@",getValues[@"comment"]]; 
return cell; 
} 
+0

返されるJsonは2つのオブジェクト –

+0

を持つ配列です。配列の2番目のオブジェクトを取得し、それを辞書として保存する必要があります。 –

+0

その後、私は –

答えて

2

ますNSArray jsonの2番目のオブジェクトからコメントを取得する必要があります。その後、リロードしますTableView

self.comment_ary = [[json objectAtIndex:1] objectForKey:@"comment"]; 
[self.tableView reloadData]; 
+0

ありがとう、それはうまくいきます –

0

プラザ

comment_ary = [[[json objectAtIndex:1]objectForKey:@"comment"] objectAtIndex:0]; 

を使用あなたは正しい場所で右キーのために を要求されていません

+0

なぜそれが助けになるか説明してください。 – rptwsthi

+0

@rptwsthi plz質問を読んでください。私のアンサーは上記の2つの答えと同じです。 –

+0

あなたの答えが他のものと同じ場合は、新しいものを追加するのではなく、他の回答をupvoteしてください。 – rptwsthi

2

その助けを願っていますが、この

comment_ary = [[json objectAtIndex:1]objectForKey:@"comment"]; 

ないこの

を行う必要があります
comment_ary = [json valueForKey:@"comment"]; 
関連する問題