如何在字符串数组中附加Json值? [英] How can I append Json values in sting arrays?

查看:108
本文介绍了如何在字符串数组中附加Json值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Alamofire和SwiftyJSON解析Json。我可以从网址看到所有json数据。控制台中的所有内容都很清楚,但是我需要在字符串数组中附加一些值。如您所见,json值。我创建了3个字符串数组。我需要在队伍,球员名称和奖杯后面附加数组。我将在表格视图中列出球队和球员的姓名。我需要帮助将值附加到数组中

I am parsing Json with Alamofire and SwiftyJSON. I can see all json data from url. Everything is clear in my console but I need to append some values in string arrays. As you can see json values. I create 3 string arrays. I need to append to arrays teams, player names and trophies. I will list team and player name in tableview. I need help to append values in arrays

///控制台中的json值

// json values in console

[
  {
    "id" : 1,
    "team" : "Liverpool",
    "players" : [
      {
        "id" : 2,
        "name" : "Alisson",
        "position" : "Goal Keeper",
        "number" : "13"
      },
      {
        "id" : 3,
        "name" : "Salah",
        "position" : "Forward",
        "number" : "10"
      }
    ],
    "trophies" : [
      "2019 champions league",
      "2005 champions league"
    ],
    "logoUrl" : "url"
  },
  {
    "id" : 4,
    "team" : "Real Madrid",
    "players" : [
      {
        "id" : 5,
        "name" : "Ramos",
        "position" : "Defender",
        "number" : "4"
      },
      {
        "id" : 6,
        "name" : "Benzema",
        "position" : "Forward",
        "number" : "9"
      }
    ],
    "trophies" : [
      "2018 champions league",
      "2017 champions league",
      "2016 champions league"
    ],
    "logoUrl" : "url"
  }
]




import Alamofire
import SwiftyJSON

var team = [String]()
var playerName = [String]()
var trophies = [String]()

func fetchJsonData(){
       DispatchQueue.main.async {
            Alamofire.request(url).responseData { response in
            guard let data = response.data else { return }
            do {
                let res = try JSONDecoder().decode([PageData].self, from:data)
                print(res)
            } catch  {
                print("having trouble converting it to a dictionary" , error)
            }
        }

        }
}

// this is my modal file

struct PageData: Codable { 
    let team: String?
    let players: [Player]
    let trophies: [String]
    let logoUrlL: String?
}

struct Player: Codable {
    let id: Int 
    let name,position, number: String?
}


推荐答案

您需要做的

1-声明一个像这样的数组

1- Declare an array like

var arr = [PageData]()

2-在此处分配并重新加载

2- Assign here and reload

arr = try JSONDecoder().decode([PageData].self, from:data)
self.tableView.reloadData()

3-设置数据源和委托

3- Set dataSource and delegate

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CellName
    let item = arr[indexPath.row]
    cell.teamlbl.text = item.team
    cell.playerslbl.text = item.players.map{ $0.name}.joined(separator:"-")
    cell.trophieslbl.text = item.trophies.joined(separator:"-")
    return cell
}






编辑:

  var teams = [String]()
  var players = [Player]()
  var trophies = [String]()


  arr = try JSONDecoder().decode([PageData].self, from:data)
  self.teams = arr.compactMap { $0.team }
  self.players = arr.flatMap { $0.players }.compactMap { $0.name }
  self.trophies = arr.flatMap { $0.trophies } 
  self.tableView.reloadData()

这篇关于如何在字符串数组中附加Json值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆