【问题标题】:How to solve JSON Dictionary如何解决 JSON 字典
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    您没有在正确的位置请求正确的密钥 你应该这样做

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

    不是这个

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

    【讨论】:

      【解决方案3】:

      请使用

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

      希望有帮助

      【讨论】:

      • 请解释为什么它会有所帮助。
      • @rptwsthi 请阅读问题。我的分析器与上述两个答案相同。
      • 如果您的答案与其他答案相同,请为其他答案投票,而不是添加新答案,
      • @rptwsthi 我对这两个答案都投了赞成票。检查他们和我同时回答的答案的时间。请在评论前清除所有内容
      猜你喜欢
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多