ios - 如何解决JSON字典

标签 ios objective-c json uitableview

我获取JSON并获取JSON词典图像,名称,URL。但是我想在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;
}

最佳答案

您需要从NSArray json的第二个对象获取评论,然后重新加载TableView

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

关于ios - 如何解决JSON字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39284635/

相关文章:

iOS 12 深度数据捕获错误代码 11800

iphone - 如何将 UIImage 发布到 Facebook 墙(iPhone/iPad)?

objective-c - 独立方向 Nib

ios - 找不到 Accella Mobile SDK IOS

python - 使用 Marshmallow 进行 JSON 反序列化中的嵌套类

ios - 不使用分屏调用 Swift viewWillTransitionToSize

ios - UICollection 的 IndexPath 到底是什么

iphone - AppDelegate、RootViewController 和 UIApplication 之间是什么关系?

javascript - Ajax Post 请求返回 JSON 但延迟失败

ruby-on-rails - 在 rails api 中更新嵌套模型