如何在ID中的tableView中查看此Json

时间:2019-08-18 20:02:51

标签: json swift uitableview alamofire populate

我正在尝试在我的App中使用Alamofire解析Json数据。我得到结果并将其显示在tableView中。但是,我需要在Json结果中按其id对象查看结果。例如,如果id = 4,我只需要在tableView中显示哪些包含id = 4的对象数据。

这是我的代码尝试工作的地方。还有Json数据:

Json数据:

{
    data =     (
                {
            content = "harika!! nerdeydiniz bu zamana kadar";
            createdAt =             {
                date = "2019-06-04 12:34:22.000000";
                timezone = "Europe/Istanbul";
                "timezone_type" = 3;
            };
            fortuneTeller =             {
                id = 1;
            };
            rating = 4;
        },
                {
            content = "bu kadar\U0131 da olmaz";
            createdAt =             {
                date = "2019-06-04 18:04:02.000000";
                timezone = "Europe/Istanbul";
                "timezone_type" = 3;
            };
            fortuneTeller =             {
                id = 1;
            };
            rating = 5;
        },
                {
            content = "be\U011fendim";
            createdAt =             {
                date = "2019-06-01 18:04:02.000000";
                timezone = "Europe/Istanbul";
                "timezone_type" = 3;
            };
            fortuneTeller =             {
                id = 2;
            };
            rating = 4;
        },
                {
            content = "tekrar istiyorum";
            createdAt =             {
                date = "2019-06-03 18:04:02.000000";
                timezone = "Europe/Istanbul";
                "timezone_type" = 3;
            };
            fortuneTeller =             {
                id = 2;
            };
            rating = 5;
        },
                {
            content = "tarot inan\U0131lmazd\U0131 kahveyi de g\U00f6nderece\U011fim";
            createdAt =             {
                date = "2019-06-11 18:04:02.000000";
                timezone = "Europe/Istanbul";
                "timezone_type" = 3;
            };
            fortuneTeller =             {
                id = 2;
            };
            rating = 5;
        },
                {
            content = "yorum tuttu";
            createdAt =             {
                date = "2019-06-12 18:04:02.000000";
                timezone = "Europe/Istanbul";
                "timezone_type" = 3;
            };
            fortuneTeller =             {
                id = 3;
            };
            rating = 4;
        },
                {
            content = "ahsen ne yapt\U0131n ahsen";
            createdAt =             {
                date = "2019-06-16 18:04:02.000000";
                timezone = "Europe/Istanbul";
                "timezone_type" = 3;
            };
            fortuneTeller =             {
                id = 3;
            };
            rating = 4;
        },
                {
            content = "gece gece heyecanlad\U0131m";
            createdAt =             {
                date = "2019-05-25 18:06:24.000000";
                timezone = "Europe/Istanbul";
                "timezone_type" = 3;
            };
            fortuneTeller =             {
                id = 4;
            };
            rating = 4;
        },
                {
            content = "mutlu hissediyor";
            createdAt =             {
                date = "2019-05-28 18:06:24.000000";
                timezone = "Europe/Istanbul";
                "timezone_type" = 3;
            };
            fortuneTeller =             {
                id = 4;
            };
            rating = 5;
        }
    );
    error = 0;
    message = Success;
}
    var message: String?
    var array = [getReviewsModel]()
    var homeDict = NSDictionary()

    func getReviewApi (completion: @escaping (Bool, String) -> ()) {
        let url = BaseUrl + getReviewsUrl
        if Reachability.isConnectedToNetwork() {
            getDictDataFromServer(url: url, headers: [:], completion: { (dict) in
                print (dict)
                if let err = dict.value(forKey: "error")
                {
                    let message = dict.value(forKey: "message") as? String
                    let error = err as! Bool
                    if !error {  
                        let arr = dict.value(forKey: "data") as! NSArray
                        for i in 0..<arr.count {    
                            let obj = getReviewsModel()
                            obj.homeDict(dict: arr[i] as! [String : Any])
                            self.array.append(obj)
                            //print(obj)   
                        } 
                        if (self.array.count == 0)
                        {
                            completion  (false, "Yorum bulunamadı.")
                            return
                        }
                        completion  (true, "Success") 
                    }
                    else
                    {
                        completion (false, message!)
                    }
                }
                else
                {
                    completion (false, internalServerMsg)
                }
            })
        }
        else
        {
            completion(false,noInternetMsg)
        }
    }

    func setDataForCell (index: Int, cell: reviewTableViewCell){
        let obj = array[index]
            cell.setData(obj: obj)
    }

    func numberOfItemsInSection () -> Int {
        return array.count
    }

    func fortuneTellerId (index: Int) -> Int {
        let obj = array[index].id
        return obj ?? 0
    }

和班级


    var content: String?
    var createdAt: String?
    var rating: Double?
    var id: Int?
    func homeDict (dict:[String:Any]){

        self.content = dict["content"] as? String ?? ""
        self.createdAt = (dict["createdAt"] as? NSDictionary)?.value(forKey: "date") as? String ?? ""
        self.rating = dict["rating"] as? Double
        self.id = (dict["fortuneTeller"] as? NSDictionary)?.value(forKey: "id") as? Int ?? 0
    }

} 

这是我的tableView函数:

 var viewModel_1 = DefaultFormViewModel()
    var jsonDict = NSDictionary()
    @IBOutlet weak var reviewDetailView: UIView!

    @IBOutlet weak var reviewContentView: UIView!

    @IBOutlet weak var reviewTableView: UITableView!

    @IBAction func viewReviewsAction(_ sender: Any) {
        reviewDetailView.isHidden = false
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel_1.numberOfItemsInSection()
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? reviewTableViewCell
        cell?.selectionStyle = .none

     viewModel_1.setDataForCell(index: indexPath.row, cell: cell!)

        return cell!
    }


 override func viewDidLoad() {
        super.viewDidLoad()

        reviewTableView.dataSource = self
        reviewTableView.delegate = self
        reviewTableView.separatorStyle = .none

        showActivityIndicator()

        viewModel_1.getReviewApi { (sucess, message) in
                    self.hideactivityIndicator()
                    if (sucess)
                    {
                        self.reviewTableView.reloadData()
                    }
                    else
                    {
             Utility().displayAlert(title: "Title", message: message, control: ["OK"])
                    }
                }
    }

0 个答案:

没有答案