php - 将 httpbody 作为 json 参数 swift 发布

标签 php json swift

需要发送如下格式的 JSON 响应

[{ data =   [{
        "current_time" = "2017-08-2911:08:26";
        "imei_no" = e639e129d0f3ab7l;
        "tbl_card_type" = 0;
        "tbl_category_details" = 0;


    }];
}]

使用下面的代码时

var arrayParams : [NSDictionary] = []
   let dataValue  = ["imei_no":"e639e129d0f3b7d","current_time":"2017-08-2911:08:26","tbl_offer_details":"0","tbl_card_type":"0","tbl_dashboard":"0","tbl_category_details":"0","tbl_sub_category_details":"0"]



  let params = ["data":dataValue]
   print(params)
  arrayParams.append(params as NSDictionary )
   print(arrayParams)

op参数如下

[{
    data =     {
        "current_time" = "2017-08-2911:08:26";
        "imei_no" = e639e129d0f3ab7d;
        "tbl_card_type" = 0;
        "tbl_category_details" = 0;

    };
}]

需要添加这样的参数

[{data = [{    "current_time" = "2017-08-2911:08:26";
        "imei_no" = e639e129d0f3ab7d;
        "tbl_card_type" = 0;
        "tbl_category_details" = 0; }];
}]

最佳答案

试试这个:

var arrayParams : [Any] = []
let dataValue  = ["imei_no"             :"e639e129d0f3b7d",
                  "current_time"        :"2017-08-2911:08:26",
                  "tbl_offer_details"   :"0",
                  "tbl_card_type"       :"0",
                  "tbl_dashboard"       :"0",
                  "tbl_category_details":"0",
                  "tbl_sub_category_details":"0"]



let params = ["data":[dataValue]]
print(params)
arrayParams.append(params)
print(arrayParams)

如果您对 () 感到困惑,您可以通过制作 JSON 来验证。

let data = try! JSONSerialization.data(withJSONObject: arrayParams, options: JSONSerialization.WritingOptions.prettyPrinted)

let json = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
if let json = json {
    print(json)
}

注意:您必须使用所有 swift 类

关于php - 将 httpbody 作为 json 参数 swift 发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45954146/

相关文章:

javascript - 如何从数据库获取 JSON 字符串结果以供以后使用

swift - Int with no value(将 int 设置为无值)

ios - UIApplication.sharedApplication().setStatusBarStyle() 已在 iOS 9 中弃用

php - Curl 在 Codeigniter 中失败 : OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to fcm. googleapis.com:443

php - yii2: 在打印中水平对齐 div?

java - JSON 异常 - 所需参数没有值

swift - 文本与 TextView swift 中的自定义按钮重叠

php - MySQL INSERT 语句未执行

php - 如何使用 PHP 和 MySQL 读取 Base64 格式的 blob?

jquery - 解析 ASP.Net 中的 JSON 对象数组