我的json响应是
{
"success" : "1",
"totalpage" : "2",
"blogdata" : [
{
"int_glocode" : "18",
"var_blogtitle" : "ssss",
"tblogdescription" : "ssssss",
"var_images" : "http//url",
"dt_creatdate" : "21-8-2017",
"flag" : "false"
}
{
"int_glocode" : "19",
"var_blogtitle" : "ssss",
"tblogdescription" : "ssssss",
"var_images" : "http//url",
"dt_creatdate" : "21-8-2017",
"flag" : "false"
}
]
}
我的代码是这个。我在这段代码中使用alamofire和swiftyjson
struct Data:Decodable {
var var_image: String
var tblogdescription : String
var var_blogtitle: String
init(blogdata:[String:Any]) {
self.var_blogtitle = blogdata["var_blogtitle"] as! String
self.tblogdescription = blogdata["tblogdescription"] as! String
self.var_image = blogdata["var_image"] as! String
}
}
我采用此数组来存储数据
var datadetail = [Data]()
然后我的json部分是这个
Alamofire.request("url", method: .post, parameters: ["API":"get_blog_list","user_id":"18","page":"1"], encoding: URLEncoding.default).responseJSON { (response) in
guard let datastring = response.result.value else { return }
if let data = datastring as? [String: Any] {
if let data1 = data["blogdata"] as? [[String:Any]] {
for data2 in data1 {
if let data3 = data2 as? [String: Any] {
let var_blogtitle = data3["var_blogtitle"] as? String
let var_image = data3["var_image"] as? String
let tblogdescription = data3["tblogdescription"] as? String
self.datadetail.append(Data(blogdata: tblogdescription))
self.datadetail.append(Data(blogdata: var_image))
self.datadetail.append(Data(blogdata: var_blogtitle))
}
else{
print("Error")
}
}
}
}
}
}
我想将var_blogtitle,var_image,tblogdescription的数据输入到结构中,以便将数据显示到表视图中。我尝试此代码,我得到了答复。我得到了var_blogtitle,var_image,tblogdescription数据,但在附加部分中出现了错误。我认为我的结构部分是错误的。请帮帮我。
预先谢谢你。
答案 0 :(得分:1)
更改您的结构名称
struct Values:Decodable {
var var_image: String
var tblogdescription : String
var var_blogtitle: String
init(blogdata:[String:Any]) {
self.var_blogtitle = blogdata["var_blogtitle"] as! String
self.tblogdescription = blogdata["tblogdescription"] as! String
self.var_image = blogdata["var_image"] as! String
}
}
和
Alamofire.request("url", method: .post, parameters: ["API":"get_blog_list","user_id":"18","page":"1"], encoding: URLEncoding.default).responseJSON { (response) in
guard let datastring = response.result.value else { return }
if let data = datastring as? [String: Any] {
if let data1 = data["blogdata"] as? [[String:Any]] {
for data2 in data1 {
if let data3 = data2 as? [String: Any] {
self.datadetail.append(Values(blogdata: data3))
}
else{
print("Error")
}
}
}
}
}
}
tableview部分是这个
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datadetail.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! detailsTableViewCell
let value = datadetail[indexPath.row]
cell.descriptionlabel.text = value.tblogdescription
cell.titlelabel.text = value.var_blogtitle
return cell
}
答案 1 :(得分:0)
如果您不确定自己的api是否有价值,请始终设置为可选
<mat-form-field>
<mat-select formControlName="productId" matInput placeholder="Product" #productId required>
<mat-option *ngFor="let a of productArray" value="{{ a._id }}" (click)="getPrice(i, a.productPrice, a.productQuantity); getTotal(i)">{{ a.productName }}</mat-option>
</mat-select>
</mat-form-field>
如果仍然出现错误,请编辑问题并显示错误
答案 2 :(得分:0)
使用以下格式的结构进行响应。如果不确定是否每次都会出现这些属性,请将其保留为可选。
struct Response: Decodable {
var success: String
var totalpage: String
var blogdata: [BlogData]
}
struct BlogData: Decodable {
var int_glocode: String
var var_blogtitle: String?
var tblogdescription: String?
var var_images: String?
var dt_creatdate: String?
var flag: String
}
只需将您的Response
模式中的响应解析为:
var responseModal = Response()
if let data = response.result.value as? [String: Any] {
do {
let jsonData = try JSONSerialization.data(withJSONObject: data, options: .prettyPrinted)
responseModal = try JSONDecoder().decode(Response.self, from: jsonData)
} catch {
print(error)
}
}