首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将JSON对象的数组作为参数值发送到请求体ios swift中

如何将JSON对象的数组作为参数值发送到请求体ios swift中
EN

Stack Overflow用户
提问于 2022-05-13 12:28:16
回答 2查看 825关注 0票数 0

你好,在api的文档中,我使用的是发票生成器API输入https://invoice-generator.com/developers#json-input,这里提到items键以一个JSON对象数组作为值。

这是我对身体的快速字典表示。

代码语言:javascript
复制
let parameters: [String: Any] = [
        "from":"Invoiced, Inc.",
        "to": "Acme, Corp.",
        "logo":"https://invoiced.com/img/logo-invoice.png",
        "number":1,
        "items":["name":"starter plan",
                 "quantity":1,
                "unit_cost":99],
        "notes":"Thanks for your business!"
    ]

问题是,当我发送空项数组时,我得到发票并在PDFView中显示它

代码语言:javascript
复制
item:[]

但是当我用数组中的一些值发送时,我从服务器得到无效的响应。

如何将JSON对象数组作为API请求体中项键的值

EN

回答 2

Stack Overflow用户

发布于 2022-05-13 12:41:27

您需要将一个对象编码为JSON。首先,您应该创建对象:

代码语言:javascript
复制
struct JSONParameters {
    let from: String
    let to: String
    let logo: String
    let number: Int
    // ... all other parameters
}

然后,将其编码到JSON并将其添加到请求主体:

代码语言:javascript
复制
            do {
                let parameters = JSONParameters(from: "Invoiced Inc.", to:......)   // Complete object
                let json = try JSONEncoder().encode(parameters)
                print("Encoded JSON: \(String(data: json, encoding: .utf8)!)")

                var request = URLRequest(url: yourURLHere)
                request.httpBody = json

                let (data, response) = try await URLSession.shared.data(for: request) // Or whatever function you need to use

                // Manage response

            } catch {
                print("\n-->> Error trying to encode : \(error) - \(String(describing: parameters))")
            }
票数 0
EN

Stack Overflow用户

发布于 2022-05-14 07:18:25

输出:-

[“数字”:1,"to":"Acme,Corp..“,"notes":”感谢您的业务!“,"from":"Invoiced,Inc.","https://invoiced.com/img/logo-invoice.png"”,"items":[ "quantity":1,"name":“初学者计划”,"unit_cost":99,"quantity":1,"name":"Starter plan2","unit_cost":99 ] struct BodyModel: Codable { var from,to: String?var徽标: String?变量数: Int?物品:物品?变量注释:字符串?func toDict() ->字符串:任何{ var字典= String:Any != nil {字典“从”= if } if != nil {->“to”=“= to } if徽标!= nil {字典”“徽标”=String:Any}如果编号!= nil {字典“number“= number } if items!= nil { var arrOfDict = [String:Any] for item!{ arrOfDict.append(item.toDict()) }字典”items“= arrOfDict } if notes != nil {字典”notes“=notes}返回字典}结构项:可编码的{ var名称: String?变量,unit_cost: Int?toDict() ->字符串:任何{ var字典= String:Any (如果名称) != nil {字典“名称”=名称}(如果数量!= nil {字典“quantity”= quantity } if unit_cost != nil {字典“unit_cost”=unit_cost}}类)ViewController: UIViewController { viewDidLoad() { super.viewDidLoad() var arrItems = Item arrItems.append(项目名称:“初学者计划”),数量: 1,unit_cost: 99)arrItems.append(名称:"Starter plan2",quantity: 1,unit_cost: 99)让plan2=BodyModel(从:"Invoiced,Inc.“到"Acme,Corp..”,商标:"https://invoiced.com/img/logo-invoice.png",number: 1,商品: arrItems,备注:“谢谢你的生意!”设参数: String: Any = body.toDict() print(参数)}}

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

https://stackoverflow.com/questions/72229610

复制
相关文章

相似问题

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