post请求不添加对象的内部数组

问题描述 投票:0回答:1

我试图在其中一个应用程序中添加帖子。

我尝试使用RestClient,它工作正常。

http://myweb.com/API/Posts/AddPost

{
"TypeId": 1, 
"UserId": 9, 
"Email": "",
"appLang": "en", 
"IsGallery": false, 
"Videos": [
{"IsFront": true, "Video": "frontVideo.mp4"},
{"IsFront": false, "Video": "backVideo.mp4"}
], 
"key": "abc-def", 
"Title": "Post 1", 
"Longitude": "", 
"userId": "9", 
"Id": 0, 
"Latitude": "", 
"Website": "", 
"ContactNo": "", 
"Photos": [
{"IsFront": true, "Photo": "frontImage.png"},
{"IsFront": false, "Photo": "backImage.png"}
], 
"Hashtag": ""
}

这个回复我的输出,如下所示。

[
  {
    "Id": 25,
    "UserId": 9,
    "Name": " ",
    "UserName": "ff006",
    "Phone": "+974 1234566",
    "DisplayName": "dff006",
    "UserPhoto": "http://myweb.com//Files/UserPhotos/t2z8mryE1KpM9o.jpg",
    "Title": "Post 1",
    "Hashtag": "",
    "Longitude": "",
    "Latitude": "",
    "Website": "",
    "Email": "",
    "ContactNo": "",
    "FromDateString": "",
    "FromDate": null,
    "ToDateString": "",
    "ToDate": null,
    "ViewersCount": 0,
    "FavoritesCount": 0,
    "IsFollowing": false,
    "IsFollowingPending": false,
    "IsFavorite": false,
    "IsReport": false,
    "AdminAdded": false,
    "Photo": "http://myweb.com//Files/PostPhotos/frontImage.png",
    "AddedOnDate": "18/09/2018 04:07 PM",
    "AddedOnFormatted": "04:07 PM",
    "AddedOn": "2018-09-18T16:07:39.923",
    "PostPhotos": [
      {
        "PostPhotoId": 10,
        "Photo": "http://myweb.com//Files/PostPhotos/frontImage.png",
        "IsFront": true
      },
      {
        "PostPhotoId": 11,
        "Photo": "http://myweb.com//Files/PostPhotos/backImage.png",
        "IsFront": false
      }
    ],
    "PostVideos": [
      {
        "PostVideoId": 9,
        "Video": "http://myweb.com//Files/PostVideos/frontVideo.mp4",
        "IsFront": true
      },
      {
        "PostVideoId": 10,
        "Video": "http://myweb.com//Files/PostVideos/backVideo.mp4",
        "IsFront": false
      }
    ]
  }
]

现在我以编程方式添加,但是我在帖子照片和视频对象中有问题。

以下是我的尝试。

我已经创建了如下对象,并尝试调用上面提到的http://myweb.com/API/Posts/AddPost的webservice

var param = Dictionary<String, Any>()

var photos = [[String: Any]]()
photos.append(["IsFront": true, "Photo": frontPhotoName])
photos.append(["IsFront": false, "Photo": backPhotoName])

var videos = [[String: Any]]()
videos.append(["IsFront": true, "Video": frontVideoName])
videos.append(["IsFront": false, "Video": backVideoName])

param["Id"] = 0
param["appLang"] = "myLang".localized()
param["UserId"] = Int(UserDefaults.standard.string(forKey: "userId")!)
param["TypeId"] = 1
param["Title"] = postTitleTV.text!
param["Hashtag"] = ""
param["Latitude"] = (currLat.containsIgnoringCase(find: "0.00") ? "" : currLat)
param["Longitude"] = (currLong.containsIgnoringCase(find: "0.00") ? "" : currLong)
param["Website"] = websiteTF.text!
param["Email"] = emailTF.text!
param["ContactNo"] = phoneTF.text!
param["IsGallery"] = false
param["Photos"] = photos
param["Videos"] = videos
param["key"] = APIKey

这个添加帖子但是当我尝试发帖时,它不会返回我的空数组,如下所示。

[
  {
    "Id": 26,
    "UserId": 9,
    "Name": " ",
    "UserName": "ff006",
    "Phone": "+974 1234566",
    "DisplayName": "dff006",
    "UserPhoto": "http://myweb.com//Files/UserPhotos/t2z8mryE1KpM9o.jpg",
    "Title": "Post 1",
    "Hashtag": "",
    "Longitude": "",
    "Latitude": "",
    "Website": "",
    "Email": "",
    "ContactNo": "",
    "FromDateString": "",
    "FromDate": null,
    "ToDateString": "",
    "ToDate": null,
    "ViewersCount": 0,
    "FavoritesCount": 0,
    "IsFollowing": false,
    "IsFollowingPending": false,
    "IsFavorite": false,
    "IsReport": false,
    "AdminAdded": false,
    "Photo": "",
    "AddedOnDate": "18/09/2018 04:13 PM",
    "AddedOnFormatted": "04:13 PM",
    "AddedOn": "2018-09-18T16:13:58.303",
    "PostPhotos": [

    ],
    "PostVideos": [

    ]
  }
]

“照片和视频”对象为空。

"PostPhotos": [

],
"PostVideos": [

]

知道我做错了什么吗?

我在控制台上打印的数据如下所示。

["appLang": "en", "UserId": 9, "TypeId": 1, 
    "Email": "", "IsGallery": false, 
    "Videos": [["IsFront": true, "Video": "frontVideo.mp4"], ["IsFront": false, "Video": "backVideo.mp4"]], 
    "key": "abc-def", "Title": "Post 1", "Longitude": "", 
    "userId": "9", "Id": 0, "Latitude": "", "Website": "",  "ContactNo": "",
     "Photos": [["IsFront": true, "Photo": "frontImage.png"], ["IsFront": false, "Photo": "backImage.png"]], 
    "Hashtag": ""]

我还打印param作为dump(param)并获得低于输出。

▿ 15 key/value pairs
  ▿ (2 elements)
    - key: "appLang"
    - value: "en"
  ▿ (2 elements)
    - key: "UserId"
    - value: 9
  ▿ (2 elements)
    - key: "TypeId"
    - value: 1
  ▿ (2 elements)
    - key: "Email"
    - value: ""
  ▿ (2 elements)
    - key: "IsGallery"
    - value: false
  ▿ (2 elements)
    - key: "Videos"
    ▿ value: 2 elements
      ▿ 2 key/value pairs
        ▿ (2 elements)
          - key: "IsFront"
          - value: true
        ▿ (2 elements)
          - key: "Video"
          - value: "frontVideo.mp4"
      ▿ 2 key/value pairs
        ▿ (2 elements)
          - key: "IsFront"
          - value: false
        ▿ (2 elements)
          - key: "Video"
          - value: "backVideo.mp4"
  ▿ (2 elements)
    - key: "key"
    - value: "abc-def"
  ▿ (2 elements)
    - key: "Title"
    - value: "Post 1"
  ▿ (2 elements)
    - key: "Longitude"
    - value: ""
  ▿ (2 elements)
    - key: "Id"
    - value: 0
  ▿ (2 elements)
    - key: "Latitude"
    - value: ""
  ▿ (2 elements)
    - key: "Website"
    - value: ""
  ▿ (2 elements)
    - key: "ContactNo"
    - value: ""
  ▿ (2 elements)
    - key: "Photos"
    ▿ value: 2 elements
      ▿ 2 key/value pairs
        ▿ (2 elements)
          - key: "IsFront"
          - value: true
        ▿ (2 elements)
          - key: "Photo"
          - value: "frontImage.png"
      ▿ 2 key/value pairs
        ▿ (2 elements)
          - key: "IsFront"
          - value: false
        ▿ (2 elements)
          - key: "Photo"
          - value: "backImage.png"
  ▿ (2 elements)
    - key: "Hashtag"
    - value: ""

知道我做错了什么吗?

请注意,我使用alamofire执行webservice。

swift http-post swift4 alamofire
1个回答
0
投票

我刚刚为Alamofire请求添加了ParameterEncoding的编码(如下所示)并且它有效

JSONEncoding.default
© www.soinside.com 2019 - 2024. All rights reserved.