ankur kumawat ankur kumawat - 3 months ago 20
iOS Question

How to solve JSON Dictionary

I fetched JSON and get in JSON Dictionary image, name, url. But i want get comment in JSON dictionary and reload in

tableView
. I tried but get null value. so please help how to get comment and reload in tableview. please below check my code and tell where I am wrong. Thank you.

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";
}
);
}
)


GET RESPONSE IN ViewDidLoad

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


TableView Methods

- (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;
}

Answer

You need to get comment from second object of NSArray json and after that reload your TableView.

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