代码之家  ›  专栏  ›  技术社区  ›  K.Wu

字典对象数组在发送到firebase cloud函数后是否会发生变化?

  •  2
  • K.Wu  · 技术社区  · 7 年前

    我有一个字典对象数组,叫做 assets ,以swift打印时,显示:

    [
        ["assetType": "video", "assetPath": "some_path"],
        ["assetType": "image", "assetPath": "some_other_path"]
    ]
    

    这是完美的,但是,当我把它发送到我的FireBase Cloud函数并在Cloud函数中打印出来之后, 资产 变成:

    [
        {
            "assetType": ["video", "image"],
            "assetPath": ["some_path", "some_other_path"]
        }
    ]
    

    为什么会这样?我该如何解决这个问题?

    ---------------------更新---

    我用 Alamofire 执行HTTP请求的模块:

    Alamofire.request(
        "https://....",
        method: .post,
        parameters: [
            "assets": assets
        ]
    )
    

    更新2---

    我的云功能如下:

    exports.testFunction = functions.https.onRequest((req, res) => {
        const { assets } = req.body;
        return res.status(200).send(assets)
    })
    

    它会立即送回 资产 一接到电话

    1 回复  |  直到 7 年前
        1
  •  2
  •   K.Wu    7 年前

    更新日期: Alamofire 这样做的方法是在底部

    我尝试了其他方法,只能回答问题的第二部分: 我该如何解决这个问题? . 显然,不使用 阿拉莫菲尔 解决了问题,但我确信没有什么问题 阿拉莫菲尔 ,可能有东西不见了,我会多挖一些,但现在,我可以选择 阿拉莫菲尔 URLSession

    let session = URLSession.shared
    let url: URL = URL(string: "https://...")!
    let request = NSMutableURLRequest(url: URL)
    request.httpMethod = "POST"
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    
    do {
        request.httpBody = try JSONSerialization.data(
            withJSONObject: [
                "assets": assets
            ],
            options: JSONSerialization.WritingOptions()
        )
        let task = session.dataTask(with: request as URLRequest) { (data, _, _) in
            if let responseData = data {
                print(String(data: responseData, encoding: .utf8)!)
            }
        }
        task.resume()
    } catch {
        print(error)
    }
    

    它可以打印:

    [
        {
            "assetType": "video",
            "assetPath": "some_path"
        },
        {
            "assetType": "image",
            "assetPath": "some_other_path"
        }
    ]
    

    ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————---

    here 显然,我应该补充一点 encoding 其他选项:

    Alamofire.request(
        "https://...",
        method: .post,
        parameters: [
            "assets": assets
        ],
        encoding: JSONSerialization(options: [])
    )
    

    这个问题会解决的。