在 UILabel 中显示 json 响应

Display json response in to 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 上,我不知道这背后的问题是什么。

如果 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];
 NSNumber * num = [[jsonDisplay objectAtIndex:0]valueForKey:@"no_of_files"];
viewController.numberofFiles.text = [num stringValue];

尝试

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

我注意到键 no_of_files 的值不是 nsstring。还需要给 .text 而不是标签赋值。 :)

使用 Alamofire 首先你必须将数据(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

 })