【问题标题】:Post json encode parameter swift发布 json 编码参数 swift
【发布时间】:2016-07-09 03:44:30
【问题描述】:

你能建议我如何在 swift 或 Objective c 中传递以下参数

         let parameters = ["Access_Key":"0699DADD8A2B5FC2E8FF6FF5DDFE03EEDB3ED2132B29EA4F28","Packages":["Length":self.Length,"Type":"","Kg":self.Weight,"Height":self.Height,"Width":self.Width,"Name":self.SkuName],
                      "issignaturerequired":"true",
                      "Outputs":["LABEL_PNG_100X175"],
                      "DeliveryReference":"86882",
                      "Destination":["ContactPerson":self.CustomerFirstname,"Address":["PostCode":self.PostalCode,"StreetAddress":self.Address1,"Suburb":self.City,"BuildingName":"","City":self.City,"CountryCode":"AU"],"PhoneNumber":"","Email":self.CustomerEmail,"Name":self.CustomerFirstname],
                      "PrintToPrinter":"true",
                      "SendTrackingEmail":"",
        "Commodities":["UnitKg":"1.0000","Currency":"NZD","Country":"NZ","Units":"1","Description":"Drums Drum Accessories Drum Bags","UnitValue":"44.0"],
        "DeliveryInstructions":"None"
                     ]
    Alamofire.request(.POST, "http://api.omniparcel.com/labels/printcheapestcourier",parameters: parameters, encoding: .JSON)
        .responseJSON { response in
            print(response)
    }
}

我是 iOS 新手

【问题讨论】:

  • 对 Objective-C 使用“AFNetworking”,对 swift 使用“Alamofire”。你可以在 github 上找到这两个库
  • 我知道,但你能建议我如何发布儿子编码参数
  • 见$attachment参数
  • 如果您将请求序列化程序设置为 JSON 并将其放入字典并作为参数传递,子 JSON 将其放入单独的字典中并将其设置为“Packages”键。我没有尝试过,但应该可以:)
  • 类似 NSMutableDictionary *subJSon = ------ NSMutableDictionary *paramerters = @{@"Packages":subJson}

标签: ios objective-c json swift swift2


【解决方案1】:

我建议您在 Swift 中为 JSON 对象使用 EVReflection

只需为您的 JSON 对象创建一个对象,然后使用 myJsonObject.toDictionary() 作为参数将其传递给 Alamofire。

【讨论】:

  • 我要发这个类型
  • 我认为您在请求方法中错过了这个:parameters: parameters as? [String : AnyObject] 这也是您的响应 JSON:response.result.value,尝试打印它
  • 我收到“无法序列化 JSON。输入数据为零或零长度”错误
  • 这适用于您从服务器获得的响应,而不适用于您发送的内容。检查网址可能吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
相关资源
最近更新 更多