我是swift的新手。这是一个反序列化的JSON
Optional({
data = (
{
comments = (
{
comments = good;
"created_date" = "2018-01-01";
id = 2;
likes = 0;
"news_id" = 4;
status = 1;
"user_id" = 5;
}
);
date = 1510167600000;
id = 4;
images = "0c980d3f9cd0393a46bb04995d16177a.jpg";
likes = 25;
title = facebook;
totalcoments = 1;
},
{
comments = (
{
comments = "Good News";
"created_date" = "2018-01-01";
id = 1;
likes = 2;
"news_id" = 7;
status = 1;
"user_id" = 5;
},
{
comments = "well good";
"created_date" = "2018-01-02";
id = 3;
likes = 0;
"news_id" = 7;
status = 1;
"user_id" = 5;
},
{
comments = "well good";
"created_date" = "2018-01-02";
id = 4;
likes = 0;
"news_id" = 7;
status = 1;
"user_id" = 5;
},
{
comments = "well good";
"created_date" = "2018-01-03";
id = 5;
likes = 0;
"news_id" = 7;
status = 1;
"user_id" = 5;
}
);
date = 1511377200000;
id = 7;
images = "0c980d3f9cd0393a46bb04995d16177a.jpg";
likes = 3;
title = don;
totalcoments = 4;
},
{
comments = (
);
date = 1510513200000;
id = 8;
images = "0c980d3f9cd0393a46bb04995d16177a.jpg";
likes = 90;
title = Dell;
totalcoments = 0;
},
{
comments = (
);
date = 1515351600000;
id = 14;
images = "0c980d3f9cd0393a46bb04995d16177a.jpg";
likes = 0;
title = dfsfdsfdfs;
totalcoments = 0;
}
);
message = "Success.";
newsurl = "http://dev.nsol.sg/projects/sneakers/assets/brand_images/thumb/";
success = 1;
totalnews = 4;
})
在下面的课程中,我试图解析json
import UIKit
class getData: NSObject {
var descriptionn : String = ""
var image : String = ""
var likes : String = ""
var comments : Int = 0
var com:String=""
var like:String = ""
func getDataForTableView(results: [[String:Any]], index : Int){
var productArray = [String:Any]()
productArray = results[index]
descriptionn = productArray["title"]! as! String
image = productArray["images"]! as! String
likes=productArray["likes"]! as! String
comments=productArray["totalcoments"]! as! Int
/*{
comments = (
{
comments = good;
"created_date" = "2018-01-01";
id = 2;
likes = 0;
"news_id" = 4;
status = 1;
"user_id" = 5;
}
);*/
//code for parsing the "comments" section of json is
let comment=productArray["comments"] as! [String:Any]?
com = comment!["comments"] as! String
like = comment!["likes"] as! String
}
}
填充表格视图单元格的代码在
之下 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "discoveryNewscell") as! DiscoveryNewsTableViewCell
classObject.getDataForTableView(results: results, index: indexPath.row)
cell.sneakerImageView.image=filteredsneakernews[indexPath.row].image
cell.newsTitle.text = classObject.descriptionn
cell.likes.text=classObject.likes
cell.comments.text=String(classObject.comments)
let imageURLPathString = newsurl + classObject.image
print("comments")
print(classObject.image)
let url1 = URL(string: imageURLPathString)
print("xyz", url1)
let data = try? Data(contentsOf: url1!)
if let imageData = data {
let image = UIImage(data: imageData)
cell.sneakerImageView.image = image
}
return cell
}
这是didSelectRowAt索引路径功能
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storybaord=UIStoryboard(name: "Main", bundle: nil)
let tabBar=storybaord.instantiateViewController(withIdentifier: "tabBar") as! UITabBarController
let DVC = tabBar.viewControllers?[0] as! NewsViewController
classObject.getDataForTableView(results: results, index: indexPath.row)
let imageURLPathString = newsurl + classObject.image
let url1 = URL(string: imageURLPathString)
print("xyz", url1)
let data = try? Data(contentsOf: url1!)
if let imageData = data {
let image = UIImage(data: imageData)
DVC.getImage = image!
}
let news = classObject.descriptionn
DVC.getNews = news
DVC.getnumberOfComment=classObject.comments
print("comment")
//code for passing "comment" part of json to view controller
print(classObject.like)
print(classObject.com)
DVC.getlikes=classObject.like
DVC.getcomment=classObject.com
self.navigationController?.pushViewController(tabBar, animated: true)
}
我想从表格视图中选择一个特定的行时,它应该在“Likes Screen”视图控制器上显示来自JSON嵌套部分的“likes”和“comments”的数量
comments = (
{
comments = good;
"created_date" = "2018-01-01";
id = 2;
likes = 0;
"news_id" = 4;
status = 1;
"user_id" = 5;
}
);
如何在JSON中读取和解析嵌套的“注释”?您可以从此链接下载项目。https://drive.google.com/file/d/1VYM4nywaBYDP2BcyBYPPNUYAu_18mhd9/view?usp=sharing