在UILabel中显示json响应

时间:2015-06-09 11:29:25

标签: ios json uilabel

我的json回复是这样的..

{
sentdetails =     (
            {
        coursename = "Course Two";
        createddate = "05/12/2015 06:13 AM";
        currentprice = "2.00";
        "no_of_files" = 9;
        queryid = 36;
        querystatus = Open;
        submissionid = TSAb13d585e;
        testsaviornotes = None;
        title = Test;
        usernotes = rdgbv;
    }
);
status = Sucess;}

现在我正在尝试显示json数据,即第一个,即“no_of_files”到UILabel,我的代码就像..

NSMutableArray * aryy=[NSJSONSerialization JSONObjectWithData:responseObject  options:kNilOptions error:&error];
     jsonDisplay = [aryy valueForKey:@"sentdetails"];
     NSLog(@"%@",jsonDisplay);
     NSLog(@"\n\n\n%@",aryy);

     SentItemDetailViewController *viewController=[self.storyboard instantiateViewControllerWithIdentifier:@"SentItemDetailViewController"];

     viewController.numberofFiles = [[jsonDisplay objectAtIndex:0]valueForKey:@"no_of_files"];

但是“num_of_files”的值无法存储在UILable上我不知道背后的问题是什么。

4 个答案:

答案 0 :(得分:2)

如果numberofFilesUILabel,您应该写viewController.numberofFiles.text,所以:

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:&error];
jsonDisplay = dict[@"sentdetails"];
NSLog(@"%@",jsonDisplay);
NSLog(@"\n\n\n%@",dict);
SentItemDetailViewController *viewController=[self.storyboard instantiateViewControllerWithIdentifier:@"SentItemDetailViewController"];
viewController.numberofFiles.text = [[jsonDisplay objectAtIndex:0][@"no_of_files"] stringValue];

答案 1 :(得分:2)

 NSNumber * num = [[jsonDisplay objectAtIndex:0]valueForKey:@"no_of_files"];
viewController.numberofFiles.text = [num stringValue];

答案 2 :(得分:2)

尝试

viewController.numberofFiles.text = [NSString stringWithFormat:@"%@",[[jsonDisplay objectAtIndex:0]valueForKey:@"no_of_files"]];

我可以注意到关键no_of_files的值不是nsstring。还需要将valuye赋予.text not label。 :)

答案 3 :(得分:0)

使用 Alamofire 首先,您必须将data(NSData)转换为字符串。

    var jsonString:String = ""
    var parseDataToShow:Data?
    @IBOutlet weak var lblToPrintDetails: UILabel!

 Alamofire.request(url, method: .post, parameters: parameter)
            .validate()
            .responseJSON(completionHandler: { (response) in

  debugPrint(response.result.value!)
// data form
  let jsonData1 = response.data 

   jsonString = String(data: parseDataToShow!, encoding: .utf8)!

    // you can print converted string data

    print("JsonString = \(jsonString)")
  self.lblToPrintDetails.text = jsonString

 })