【发布时间】: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