首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在iOS中使用json响应的键和值?

如何在iOS中使用json响应的键和值?
EN

Stack Overflow用户
提问于 2019-09-10 09:58:05
回答 1查看 524关注 0票数 0

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

以下是我的json答复:

代码语言:javascript
复制
    {
"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的回应:

代码语言:javascript
复制
   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

这是我的模型:

代码语言:javascript
复制
 import SwiftyJSON

 struct poItems {

    var key: String?
    var value: String?
  }

下面是我传递给表视图的静态值:

代码语言:javascript
复制
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

任何帮助--非常感谢--请..。

EN

回答 1

Stack Overflow用户

发布于 2019-09-10 10:44:09

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

results的值是一个数组

代码语言:javascript
复制
if let categories = json["d"]["results"].array {
    print("category::\(self.categories)")
    for category in categories {
        for (key, value) in category {
           print(key, value)
        }
    }
}

而且--正如您在前面的许多问题和评论中所建议的--我们鼓励您放弃SwiftyJSON并使用Codable

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57868335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档