【问题标题】:How to use key and value from json response in tableview in iOS swift?如何在iOS swift的tableview中使用json响应中的键和值?
【发布时间】:2019-09-10 09:58:05
【问题描述】:

在 tableview 中,每个带有键和值标签的单元格,我可以将静态键值传递给 tableview 但现在尝试从 Json 响应中获取数据以显示在 tableview.

这是我的 json 响应:

    {
"d": {
    "results": [
        {
            "__metadata": {
                "id": "http://192.168.41.27:8009/sap/opu/odata/sap/Z_MM_PO_01_SRV/POItemSetSet('4500022401')",
                "uri": "http://192.168.41.27:8009/sap/opu/odata/sap/Z_MM_PO_01_SRV/POItemSetSet('4500022401')",
                "type": "Z_MM_PO_01_SRV.POItemSet"
            },
            "PoDocNo": "4500022401",
            "Item": "00010",
            "Material": "RMECC_MOB1",
            "StorageLocation": "3001",
            "MatGroup": "00107",
            "Quantity": "2.000",
            "OrderUnit": "KG",
            "NetPrice": "1000.000",
            "UnitofPrice": "1.000",
            "ItemCat": "0",
            "Requistor": ""
        },
        {
            "__metadata": {
                "id": "http://192.168.41.27:8009/sap/opu/odata/sap/Z_MM_PO_01_SRV/POItemSetSet('4500022401')",
                "uri": "http://192.168.41.27:8009/sap/opu/odata/sap/Z_MM_PO_01_SRV/POItemSetSet('4500022401')",
                "type": "Z_MM_PO_01_SRV.POItemSet"
            },
            "PoDocNo": "4500022401",
            "Item": "00020",
            "Material": "RMECC_MOB1",
            "StorageLocation": "3001",
            "MatGroup": "00107",
            "Quantity": "2.000",
            "OrderUnit": "KG",
            "NetPrice": "1000.000",
            "UnitofPrice": "1.000",
            "ItemCat": "0",
            "Requistor": ""
        }
     ]
   }
  }

这里我得到 json 响应:

   extension PoItemDetailsViewController {

func GetPoItemCount() {

      if orderNo != nil {
    // Call API
        print("orderni::\(orderNo!)")
        PoVendorListApiManager.sharedInstance.getPoListWithModel(orderString: orderNo!){ (json:JSON) in
        // return json from API
            if let categories = json["d"]["results"].dictionary {
                print("catefory::\(self.categories)")

                for (key, value) : (String, JSON) in categories {
                    self.dict[key] = value.object as AnyObject
                }
                print("dict:::\(self.dict)")

 //                    for key in categories.keys {
 //                        if let category = 
  categories[key]?.stringValue {
   //                            
  self.categories.updateValue(category, forKey: key)
//                        }
//
//                    }
            }
            print("catefory::\(self.categories)")


        }
     }
  }

 }//extension

这是我的模型:

 import SwiftyJSON

 struct poItems {

    var key: String?
    var value: String?
  }

这是我传递给表格视图的静态值:

private var PoItems: [poItems]?
private var poVendorItemArray = [PoVendorModel]()
private func loadPoItems() -> [poItems] {

var tempItems = [poItems]()

let item1 = poItems.init(key: "Material#", value: "")
let item2 = poItems.init(key: "Quantity", value: "Bottles")
let item3 = poItems.init(key: "StorageLocation", value: "KP04")
let item4 = poItems.init(key: "PoDocNo", value: "KP Suppliers")
let item5 = poItems.init(key: "NetPrice", value: "1000")
return tempItems
}

如何将带有键和值的 json 响应动态传递到 tableview?

任何帮助都非常感谢...

【问题讨论】:

  • 你遇到了什么错误?
  • 阅读有关可编码、映射和一般的 tableview 数据源的信息。这是一个非常广泛的问题。但基本上你需要将你的 json 映射到一个模型。然后一旦数据被检索和映射。您将更新 tableview 的数据源并调用重新加载数据。

标签: arrays json swift dictionary swifty-json


【解决方案1】:

请在您的earlier question 中重新阅读我的回答(或阅读 JSON)

results 的值是一个数组

if let categories = json["d"]["results"].array {
    print("category::\(self.categories)")
    for category in categories {
        for (key, value) in category {
           print(key, value)
        }
    }
}

并且——正如你之前的许多问题和 cmets 中所建议的——我们鼓励你放弃SwiftyJSON 并使用Codable。

【讨论】:

    猜你喜欢
    • 2021-08-12
    • 2020-03-10
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-24
    • 1970-01-01
    相关资源
    最近更新 更多