快速调用解析函数失败

时间:2019-06-17 12:49:52

标签: arrays swift dictionary parse-platform

我正在开发一个食品交付应用程序,该应用程序将parse作为其后端。通过

调用placeOrder API时遇到问题

PFCloud.callFunction(inBackground: PlaceOrder, withParameters: params) { (data, err) in}

请查看我需要在下面发布的JSON。

{
 "source": "card_1EVYuOEynlyM6L4SHgBMJYRQ",
 "userId": "YjSZYSXEp7",
   "data": {
            "menuItems": [{
                            "id": "QSYa2JDcIm",
                         "title": "Rice With Tibss(Beef)",
                     "menuTitle": "Rice With Tibss",
                   "submenuItem": [{
                                    "id": "zaOo6G4KSV",
                                  "name": "Beef",
                                 "price": 12,
                                  "desc": "Fillings?"
                                   }],
                         "price": 24,
                           "qty": 1,
                       "storeId": "yqBCDmzaDP",
                     "storeName": "Ibex Ethiopian Cusine and Bar",
                     "orderType": "takeout",
                      "taxState": 0.0925,
                     "storeInfo": {
                                   "cart_storeId": "yqBCDmzaDP",
                                 "cart_storeName": "Ibex Ethiopian Cusine and Bar",
                                "cart_storeImage": "https://res.cloudinary.com/http-get-tolofood-com/image/upload/c_scale,h_199,q_auto,w_270/v1461575640/Ibex_lopx38.jpg",
                              "cart_storeCuisine": "Ethiopian",
                          "cart_storeDescription": "We always serve a quality food. We always serve a quality food. We always serve a quality food. We always serve a quality food.",
                               "cart_storeRating": 3.33,
                             "cart_storeDelivery": false,
                                        "takeout": true,
                                        "address": "12255 Greenville Ave,Dallas, TX 75243",
                                       "slugname": "TX_DAL_ibex_ethiopian_cuisine_and_bar",
                              "multiple_location": false,
                          "cart_storeDeliveryFee": 15,
                               "cart_storeServes": "Lunch,Dinner",
                                           "busy": false,
                              "cart_storeSeoSlug": "ibex-ethiopian-cusine-and-bar"
                                  },
                       "enable": true,
          "voice_read_mi_label": "fbgcb",
         "voice_read_mi_option": false,
                 "menuTypeName": "Standard"
                            }],
   "lastOrderType": "takeout",
   "searchedAddress": "takeout",
   "timeData": {
                "day": "06-05-2019",
               "time": "12:55 am",
                 "tz": "America/Los_Angeles"
               }
            },
 "unavailable_option": "restaurant_recommendation"
  }

下面是我用来传递代码的Swift代码。

let storeInfo: Dictionary = [CartStoreId: self.cartStoreId, CartStoreName: self.cartRestaurantName, CartStoreImage: self.cartStoreImage, CartStoreCuisine: self.cartStoreCuisine, CartStoreDescription: self.cartStoreDescription, CartStoreRating: self.cartStoreRating, CartStoreDelivery: self.cartStoreDelivery, Takeout: self.takeOut, Address: self.address, Slugname: self.slugName, MultipleLocation: self.multipleLocation, CartStoreDeliveryFee: self.cartStoreDelivery, CartStoreServes: self.cartStoreServes, Busy: self.busy, CartStoreSeoSlug: self.cartStoreSeoSlug] as Dictionary
let subMenuItem = ["id": "zaOo6G4KSV", "name": "Beef", "price": 12, "desc": "Fillings?", "voice_read_submi_label":"bf", "voice_read_submi_option":false, "disabled": false] as [String: Any]
let ordersDictionary = [
            "id" : "1234",
            "title" : "Test",
            "menuTitle" : "MenuName",
            "price" : 23,
            "qty" : 2,
            "storeId" : 23,
            "orderType" : "standard",
            "taxState" : 0.22,
            "enable" : true,
            "menuTypeName" : "Type Name",
            "voice_read_mi_label":"fdfs",
            "voice_read_mi_option":"false",
            "submenuitem": subMenuItem,
            "storeInfo": storeInfo
            ] as Dictionary
let timeData = ["day" : 17-06-2019, "time": "11:00 AM", "tz": "America/Los_Angeles"] as Dictionary
let data = ["menuItems": ordersDictionary, "lastOrderType": "takeout", "searchedAddress": "takeout", "timeData" : timeData] as Dictionary
let params = [UserId: self.userId, "source":"card_1EVYuOEynlyM6L4SHgBMJYRQ", "data": data, "unavailable_option":"restaurant_recommendation","_ApplicationId":"6EuadToYoFGJhI1sX8XnuFBz9tp9l3yH6HxzzXZO", "_JavaScriptKey":"rQkALu9saFtF2oq9yCibyw6mEcs3PVqct3uuP6vg", "_ClientVersion":"js1.6.14", "_InstallationId":"444ec64d-5fcc-7b8e-596e-6be627892c2a",
        "_SessionToken":"r:c966376120c8eca77aa63c29d5bebe1a"] as Dictionary

完成所有这些操作后,我将调用如下的parse函数。

PFCloud.callFunction(inBackground: PlaceOrder, withParameters: params) { (data, err) in
        if err != nil {
            print(err!)
        } else {
            print(data!)
        }
    }

但这会在几秒钟后让我出错

"Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}"

我在网上搜索了错误,并进行了相应的修复,但仍然没有成功。请帮助我。

1 个答案:

答案 0 :(得分:0)

我注意到您的params var与您发送的JSON不兼容,有更多字段,也缺少字段。此外,menuItems和submenuItem在JSON中是一个数组,在代码中是一个对象。这可能会使云代码功能失败,因此您没有收到有效的JSON。请尝试以下操作,并检查其是否有效。在这种情况下,只需将值替换为您的变量即可。

let params = [
    "source": "card_1EVYuOEynlyM6L4SHgBMJYRQ",
    "userId": "YjSZYSXEp7",
    "data": [
        "menuItems": [[
            "id": "QSYa2JDcIm",
            "title": "Rice With Tibss(Beef)",
            "menuTitle": "Rice With Tibss",
            "submenuItem": [[
                "id": "zaOo6G4KSV",
                "name": "Beef",
                "price": 12,
                "desc": "Fillings?"
            ]],
            "price": 24,
            "qty": 1,
            "storeId": "yqBCDmzaDP",
            "storeName": "Ibex Ethiopian Cusine and Bar",
            "orderType": "takeout",
            "taxState": 0.0925,
            "storeInfo": [
                "cart_storeId": "yqBCDmzaDP",
                "cart_storeName": "Ibex Ethiopian Cusine and Bar",
                "cart_storeImage": "https://res.cloudinary.com/http-get-tolofood-com/image/upload/c_scale,h_199,q_auto,w_270/v1461575640/Ibex_lopx38.jpg",
                "cart_storeCuisine": "Ethiopian",
                "cart_storeDescription": "We always serve a quality food. We always serve a quality food. We always serve a quality food. We always serve a quality food.",
                "cart_storeRating": 3.33,
                "cart_storeDelivery": false,
                "takeout": true,
                "address": "12255 Greenville Ave,Dallas, TX 75243",
                "slugname": "TX_DAL_ibex_ethiopian_cuisine_and_bar",
                "multiple_location": false,
                "cart_storeDeliveryFee": 15,
                "cart_storeServes": "Lunch,Dinner",
                "busy": false,
                "cart_storeSeoSlug": "ibex-ethiopian-cusine-and-bar"
            ],
            "enable": true,
            "voice_read_mi_label": "fbgcb",
            "voice_read_mi_option": false,
            "menuTypeName": "Standard"
        ]],
        "lastOrderType": "takeout",
        "searchedAddress": "takeout",
        "timeData": [
            "day": "06-05-2019",
            "time": "12:55 am",
            "tz": "America/Los_Angeles"
        ]
    ],
    "unavailable_option": "restaurant_recommendation"
]