【问题标题】:How To Display key and value from Json Object如何显示来自 Json 对象的键和值
【发布时间】:2019-07-23 10:23:51
【问题描述】:

我正在使用 Json 对象在 tableview 中显示。我也成功解析了 Json 打印数据。我正在使用 foreach 语句将数据放入变量但问题是我将变量放入 json 对象中的最后一项。我想显示用户名称变量以在其上显示名称

这是我的 Json 数据

{
"Categories": [
    "school, class"
   ],
"Tags": [
    {
        "Value": "ashok",
        "Key": "Name"
    }, {
        "Value": "III",
        "Key": "class"
    }, {
        "Value": "A",
        "Key": "section"
    }
   ]
}

这是我的模型数组

struct classInfo: Decodable {  
    let Categories: String
    let Tags: String
    let Value: String
    let Key: String
    var Name: String
    let class: String
}

这是我的代码

var ClassList = [classInfo]()

var name: String = ""
var class: String = ""

在JSONSerialization FUNCTION中

do{
            let json = try JSONSerialization.jsonObject(with: data!, options: []) as! [String: AnyObject]
            print(json as AnyObject)

            let cat = json["Categories"] as? [String: Any]
            print(cat!)

         if let array = json["Tags"] as? [[String: Any]] {
                for obj in array {
                    if let dict = obj as? NSDictionary {
                        // Now reference the data you need using:
                        let Value = dict.value(forKey: "Value")
                        let Key = dict.value(forKey: "Key")
                        print(Value!)
                        print(Key!)

                        self.name = tag["Value"] as! String
                        print(self.name)

                        self.class = tag["Value"] as! String
                        print(self.class)
                    }
                }
            }

行功能的单元格

    cell.Name.text = "Name:\(name)"
    cell.class.text = "class: \(class)"

【问题讨论】:

  • 名字和班级是什么,请添加更多信息???
  • 我想在json中的tableview类名中显示类名
  • name 变量更新 json 对象中的最后一项
  • 我不懂你,但是如果你需要类名你可以使用这个`print(MyClass.self)`

标签: ios json swift iphone nsjsonserialization


【解决方案1】:

嘿嘿,试试这个,

class ModelClass {var name:String}

现在创建一个自定义 tableView 单元格

class CustomTableViewCell:UITableViewCell { 
    var dataModel : ModelClass?{
           didSet{
           guard let model = dataModel else {return}
           print(model.name)
           // set label text here : ex := myLabel.text = model.name

        }
     }
   // declare all IBLayouts or create them programmatically... its your choice

}

在您的 tableView 单元格中 (cellForRowAt ---> ) 使用这个 sn -p

var dataModel = [ModelClass]()
dataModel = [ModelClass(name:"Rishabh"),ModelClass(name:"Adi"),ModelClass(name:"FFFf")]

现在使用

将此 dataModel 传递给 cellDAtaModel
let cell = tableView.deq() as! CustomTableViewCell //deq() - > to lazy to type whole lol ;P type by yourself 
cell.dataModel = dataModel[indexPath.row]

【讨论】:

  • 请帮我解决这个问题
  • 你在用什么?向我显示错误/日志将帮助我消除您的疑虑
【解决方案2】:

使用这个

class ModelClass{ var name:String ; var classs:String }

class ModelDataClass { static var modelData = [ModelClass]() }

在您的视图控制器中。 - 在模型中添加值和 在你的 cellForRowAt -->

cell.nameTextLabel.text = ModelDataClass.modelData[indexPath.row].name
cell.classsTextLabel.text = ModelDataClass.modelData[indexPath.row].classs

【讨论】:

  • 如何在tableview中显示json模型请帮帮我
猜你喜欢
  • 1970-01-01
  • 2013-05-31
  • 2021-09-26
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多