简体   繁体   中英

How to use key and value from json response in tableview in iOS swift?

In tableview, each cell with key and value labels, i can able to pass static key value to tableview but now trying to get data from Json response to show in tableview .

here is my json response:

    {
"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": ""
        }
     ]
   }
  }

here i'm getting json response:

   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

here is my model:

 import SwiftyJSON

 struct poItems {

    var key: String?
    var value: String?
  }

here is my static value i have passed to table view:

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
}

how can i pass json reponse with key and value dynamically into tableview ?

Any help much appreciates pls...

Please reread my answer in your earlier question (or read the JSON)

The value for results is an array

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

And – as suggested in many of your previous questions and in the comments – you are encouraged to drop SwiftyJSON and use Codable .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM