以JSON格式向服务器发送数据
服务器需要JSON格式的me数据。像这样,以JSON格式向服务器发送数据,json,swift,Json,Swift,服务器需要JSON格式的me数据。像这样, func getProductListResponse(typeId : String , type : String) { let tsoftFilter = [ "key" : type, "value" : typeId ] as [String : AnyObject] print(&
func getProductListResponse(typeId : String , type : String) {
let tsoftFilter = [
"key" : type,
"value" : typeId
] as [String : AnyObject]
print("tsoft filter is \(tsoftFilter)")
let serviceParams = [
"store_id" : Config.productListId,
"page" : currentPage,
"per_page" : perPage,
"tsoft_filters" : "\(tsoftFilter)"
] as [String : AnyObject]
。我试图发送这样的数据
func getProductListResponse(typeId : String , type : String) {
let tsoftFilter = [
"key" : type,
"value" : typeId
] as [String : AnyObject]
print("tsoft filter is \(tsoftFilter)")
let serviceParams = [
"store_id" : Config.productListId,
"page" : currentPage,
"per_page" : perPage,
"tsoft_filters" : "\(tsoftFilter)"
] as [String : AnyObject]
但是它不起作用,我在哪里犯错误?您正在添加一个不等于JSON的字典描述。根据截图,你甚至需要一系列字典
在这种情况下,更容易创建JSON
func getProductListResponse(typeId : String , type : String) {
let tsoftFilter = #"[{"key":"\(type)","value":"\(typeId)"}]"#
let serviceParams : [String : Any] = [
"store_id" : Config.productListId,
"page" : currentPage,
"per_page" : perPage,
"tsoft_filters" : tsoftFilter
]
...
顺便说一句,在网络请求中发送的参数字典决不是[String:AnyObject]
您添加的字典描述不等于JSON。根据截图,你甚至需要一系列字典
在这种情况下,更容易创建JSON
func getProductListResponse(typeId : String , type : String) {
let tsoftFilter = #"[{"key":"\(type)","value":"\(typeId)"}]"#
let serviceParams : [String : Any] = [
"store_id" : Config.productListId,
"page" : currentPage,
"per_page" : perPage,
"tsoft_filters" : tsoftFilter
]
...
顺便说一下,在网络请求中发送的参数字典永远不会是[String:AnyObject]
请尝试以下操作:
let tsoftFilter = """
[
[ "key" : type, "value" : typeId]
]
"""
试试这个:
let tsoftFilter = """
[
[ "key" : type, "value" : typeId]
]
"""
JSON使用大括号表示字典,Swift不使用。JSON使用大括号表示字典,Swift不使用。您有一个有效的邮递员示例。邮递员可以生成Swift代码。这不是漂亮的Swift代码,但它应该给你一个提示。你查过了吗?我不知道,谢谢你的回答。你有一个工作邮递员样本。邮递员可以生成Swift代码。这不是漂亮的Swift代码,但它应该给你一个提示。你查过了吗?我不知道,谢谢你的回答。