【问题标题】:Create JSON API Request [duplicate]创建 JSON API 请求 [重复]
【发布时间】:2017-12-21 03:36:54
【问题描述】:

我正在尝试为我的 API 请求生成 JSON 数据,请参见下面的示例:

    var itemsArray = [String:AnyObject]()
    var itemDictionary: [String: Any] = ["employee":"Max Mustermann", "table":"Tisch X"]

    for item in items{

            let item = item as! Items
            itemsArray = ["name": item.name as AnyObject, "quantity": String(item.count) as AnyObject]
        }
        itemDictionary["items"] = itemsArray


    let jsonData = [itemDictionary]
    guard let httpBody = try? JSONSerialization.data(withJSONObject: jsonData, options: []) else{
        return }

这是我的代码,但它不起作用。 我不知道这段代码有什么问题。

我希望任何人都可以帮助我。这是完成我的项目的最后一步。

【问题讨论】:

标签: json swift rest api


【解决方案1】:
 do {

let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions()) as? Dictionary<String,AnyObject>

} 
catch 
{
     return NSError(domain: "\(error)" , code: 1, userInfo: nil)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-21
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    相关资源
    最近更新 更多