在TableView对象中打印JSON数组时出现问题

时间:2018-08-09 11:40:35

标签: ios json swift uitableview alamofire

我正在调用api,我得到了正确的响应,但是我不明白如何打印对象数组,就像这是我的回应

回复

{
    "success": "1",
    "data": [
        {
            "month": 0
        },
        {
            "month": 0
        },
        {
            "month": 0
        },
        {
            "month": 0
        },
        {
            "month": 0
        },
        {
            "month": 0
        },
        {
            "month": 0
        },
        {
            "month": 3
        },
        {
            "month": 0
        },
        {
            "month": 0
        },
        {
            "month": 0
        },
        {
            "month": 0
        }
    ],
    "like_data": [
        {
            "like": 0
        },
        {
            "like": 0
        },
        {
            "like": 0
        },
        {
            "like": 0
        },
        {
            "like": 0
        },
        {
            "like": 0
        },
        {
            "like": 0
        },
        {
            "like": 6
        },
        {
            "like": 0
        },
        {
            "like": 0
        },
        {
            "like": 0
        },
        {
            "like": 0
        }
    ],
    "cmnt_data": [
        {
            "cmnt": 0
        },
        {
            "cmnt": 0
        },
        {
            "cmnt": 0
        },
        {
            "cmnt": 0
        },
        {
            "cmnt": 0
        },
        {
            "cmnt": 0
        },
        {
            "cmnt": 0
        },
        {
            "cmnt": 1
        },
        {
            "cmnt": 0
        },
        {
            "cmnt": 0
        },
        {
            "cmnt": 0
        },
        {
            "cmnt": 0
        }
    ]
}

所以我想打印我尝试过的like_data和cmnt_data让我向您展示我的代码

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! InstaAnalyticsTableViewCell
       let month = searchlist[indexPath.row]["month"]! as? Int
        print(month!)
        let optionalvalue = month
        if let noLongerOptional = optionalvalue {
            print("\(noLongerOptional) no its not! ahah")
            cell.lblMonthCount.text = "\(noLongerOptional)" as? String
        }

         let instaResponse = searchlist[indexPath.row]["like_data"] as! [String:Any]
         let likes = instaResponse["like"] as! Int
         cell.lblLikeCount.text = "\(likes)"
        //cell.lblMonthCount.text = "\(month)"
        return cell
    }

我正在如上所述尝试,但是我没有走错地方,请帮我解决这个问题,我还需要一个帮助,我希望以条形图或折线图的形式显示所有数据,所以请任何一个帮助我

看到这里我以这种方式调用api,所以我必须写哪个响应

Alamofire.request(instaPostAnalytics, method: .post, parameters: userprofile).responseJSON
                {
                    response in

                    print(response)
                    let result = response.result
                    print(result)
                    let monthArray = yourResponse["data"] as? [[String:Int]] ?? []
                    let likeArray = yourResponse["like_data"] as? [[String:Int]] ?? []
                    let cmntArray = yourResponse["cmnt_data"] as? [[String:Int]] ?? []
                    //As of all have equal object loop like this
                    for i in 0..<min(monthArray.count, likeArray.count, cmntArray.count) {
                        let month = monthArray[i]["month"] ?? 0
                        let like = likeArray[i]["like"] ?? 0
                        let cmnt = cmntArray[i]["cmnt"] ?? 0
                        datas.append(Data(month: month, like: like, cmnt: cmnt))
                    }
                    //Now reload the tableView
                    self.yourTableView.reloadData()
//                    if let dict = result.value as? Dictionary<String,AnyObject>{
//                        if let categorylist = dict["data"]{
//                            self.searchlist = categorylist as! [AnyObject]
//                            self.tblInstalytics.reloadData()
//                    }
//                }
            }

我昨天告诉我,我无法在图表上显示数据,这是我的代码

代码

        let data = response.result
        if let dict = data.value as? Dictionary<String,AnyObject>{
            if let categorylist = dict["data"]{
                self.searchlist = categorylist as! [AnyObject]

                for item in self.searchlist{
                    let value = item["month"]
                    guard let value_chart = value else {
                        continue
                    }
                    let optionalvalue = value_chart
                    if let noLongerOptional = optionalvalue {
                        print("\(noLongerOptional)")
                        let chartConfig = BarsChartConfig(valsAxisConfig: ChartAxisConfig(from: 0, to: 800, by: 100))
                        let frame = CGRect(x: 0, y: 20, width: self.view.frame.width, height: 232 )
                        let chart = BarsChart(frame: frame,
                                              chartConfig: chartConfig,
                                              xTitle: "Months",
                                              yTitle: "Count",
                                              bars: [
                                                ("Jan", noLongerOptional as! Double),
                                                ("Feb", noLongerOptional as! Double),
                                                ("Mar", noLongerOptional as! Double),
                                                ("Apr", noLongerOptional as! Double),
                                                ("May", noLongerOptional as! Double),
                                                ("Jun", noLongerOptional as! Double),
                                                ("July",noLongerOptional as! Double),
                                                ("Aug", noLongerOptional as! Double),
                                                ("Sep", noLongerOptional as! Double),
                                                ("Oct", noLongerOptional as! Double),
                                                ("Nov", noLongerOptional as! Double),
                                                ("Dec", noLongerOptional as! Double)
                            ],
                                              color: UIColor.red,
                                              barWidth: 15
                        )
                        self.view.addSubview(chart.view)
                        self.chartView = chart
                    }
                }
            }

但是我无法显示,也无法理解如何在图表中显示likeData和cmnt数据

请帮助我解决这个问题

2 个答案:

答案 0 :(得分:2)

JSON中应该有一个具有所有值的数组,而不是在{ "success": "1", "data": [ { "month": 0, "like": 0, "cmnt": 1 }, ... ] } 中有3个不同的数组。

JSON

到目前为止,使用当前的struct响应,您可以做的是制作classstruct Post { var month: Int var like: Int var cmnt: Int } 的一个数组并将其用作tableView的数据源。

Post

现在,您的控制器中的var datas = [Post]() //Initialize datas array like this when you get `JSON` let result = response.result.value as? [String : Any] ?? [:] let monthArray = result["data"] as? [[String:Int]] ?? [] let likeArray = result["like_data"] as? [[String:Int]] ?? [] let cmntArray = result["cmnt_data"] as? [[String:Int]] ?? [] //As of all have equal object loop like this for i in 0..<min(monthArray.count, likeArray.count, cmntArray.count) { let month = monthArray[i]["month"] ?? 0 let like = likeArray[i]["like"] ?? 0 let cmnt = cmntArray[i]["cmnt"] ?? 0 datas.append(Post(month: month, like: like, cmnt: cmnt)) } //Now reload the tableView self.yourTableView.reloadData() 数组上进行了声明,并将其与tableView方法一起使用。

UITableViewDataSource

现在只需更改您的func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return datas.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! InstaAnalyticsTableViewCell let data = datas[indexPath.row] cell.lblMonthCount.text = String(data.month) cell.lblLikeCount.text = String(data.like) cell.lblCmntCount.text = String(data.cmnt) return cell } 方法即可。

<div id="append">append</div>
<textarea id="text"></textarea>

答案 1 :(得分:0)

//first take 3 dataArray for taking values from the response 

var likeArr = fullresponce["like_data"] as [[String:Anyobject]]
var commentArr = fullresponce["cmnt_data"] as [[String:Anyobject]]

var like = (likeArr[indexPath.row] as Dictionary)["like"]! as? Int

var cmnt = (commentArr[indexPath.row] as Dictionary)["cmnt"]! as? Int