0

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...

2
  • what error you are facing? Commented Sep 10, 2019 at 10:11
  • Read about codable, mapping and just tableview datasource in general. This is a very broad question. But basically you need to map your json to a model. Then once the data has been retrieved and mapped. You would update the datasource of the tableview and call reload data. Commented Sep 10, 2019 at 10:24

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.