【发布时间】:2016-09-02 05:05:06
【问题描述】:
我获取 JSON 并输入 JSON 字典图像、名称、网址。但我想在 JSON 字典中获得评论并在 tableView 中重新加载。我试过但得到空值。所以请帮助如何在tableview中获得评论和重新加载。请在下面检查我的代码并告诉我哪里错了。谢谢。
我的 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 中获取响应
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 方法
- (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;
}
【问题讨论】:
-
返回的 Json 是一个包含 2 个对象的数组
-
你必须得到数组的第二个对象,然后保存为字典。
-
然后获取“comment”的值
-
@AbdulRehmanWarraich 如何解析和获取第二个对象并在 tableview 中重新加载评论和名称
-
@BhadreshKathiriya 好的,我更新了我的代码,请稍候
标签: ios objective-c json uitableview