首页 > 解决方案 > 我想在 swift 中为 post 参数创建一个 API JSON 结构

问题描述

我想创建一个嵌套 JSON 以将其用作 Post 方法参数。我尝试了 NSDictionary、Array、[String:Any] 等,但未能获得准确的 JSON 结构。我想要一个 JSON 格式,像这样

    {
  "Command": [
    {
      "commandData": {
        "name": "name",
        "description": "description",
        "title": "title",
        "actions": [
          {
            "ERR": [
              {
                "ERR_LINE": {
                  "Value": {
                    "1": 1.0,
                    "2": 1.0,
                    "3": 1.0,
                    "4": 1.0,
                    "5": 1.0,
                    "6": 1.0
                  }
                }
              }
            ]
          }
        ]
      },
      "errID": "id",
      "issueID": "id"
    }
  ]
}

标签: jsonswiftdictionaryparameters

解决方案


你可以这样做:

var postData = Dictionary<String,Any>()

postData = [

"Command": [

    "commandData": [

        "name": "name",

        "description": "description",

        "title": "title",

        "actions": [

            "ERR": [

                "ERR_LINE": [
                  "Value": [
                    "1": 1.0,
                    "2": 1.0,
                    "3": 1.0,
                    "4": 1.0,
                    "5": 1.0,
                    "6": 1.0
                  ]
                ]
            ]  
        ]
    ],
  "errID": "id",
  "issueID": "id"
  ]
]

希望它有效。


推荐阅读