title: Invalid top-level type in JSON write'
body: <p>I am passing some parameter to api , to done add to cart function. But when i pass the parameter , it showing the crash <code>Invalid top-level type in JSON write'</code> i know there is the problem in my passing parameter . Please help me out!.How to do that.Please help me out !!!</p>
  
  <p>This is the json format of parameter i am passing !!:</p>
  
  <pre><code>{
      "cartType" : "1",
      "cartDetails" : {
          "customerID" : "u",
          "cartAmount" : "6999",  
          "cartShipping" : "1",
          "cartTax1" : "69",
          "cartTax2" : "",
          "cartTax3" : "",
          "cartCouponCode" : "",
          "cartCouponAmount" : "",
          "cartPaymentMethod" : "",
          "cartProductItems" : {
              "productID" : "9",
              "productPrice" : "6999",
              "productQuantity" : "1"
          }
      }
  }
  </code></pre>
  
  <p>My updated solution  :</p>
  
  <pre><code>func addtocartapicalling ()
  {
      let headers = [
          "cache-control": "no-cache",
          "postman-token": "4c933910-0da0-b199-257b-28fb0b5a89ec"
      ]
  
      let jsonObj:Dictionary&lt;String, Any&gt; = [
          "cartType" : "1",
          "cartDetails" : [
              "customerID" : "sathish",
              "cartAmount" : "6999",
              "cartShipping" : "1",
              "cartTax1" : "69",
              "cartTax2" : "",
              "cartTax3" : "",
              "cartCouponCode" : "",
              "cartCouponAmount" : "",
              "cartPaymentMethod" : "",
              "cartProductItems" : [
                  "productID" : "9",
                  "productPrice" : "6999",
                  "productQuantity" : "1"
              ]
          ]
      ]
  
      if (!JSONSerialization.isValidJSONObject(jsonObj)) {
          print("is not a valid json object")
          return
      }
  
      if let postData = try? JSONSerialization.data(withJSONObject: jsonObj, options: JSONSerialization.WritingOptions.prettyPrinted) {
          let request = NSMutableURLRequest(url: NSURL(string: "http://expapi.php")! as URL,
                                            cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
          request.httpMethod = "POST"
          request.allHTTPHeaderFields = headers
          request.httpBody = postData
  
          let session = URLSession.shared
          let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -&gt; Void in
              if (error != nil) {
                  ///print(error)
              } else {
  
                  DispatchQueue.main.async(execute: {
  
                      if let json = (try? JSONSerialization.jsonObject(with: data!, options: [])) as? Dictionary&lt;String,AnyObject&gt;
                      {
                          let status = json["status"] as? Int;
                          if(status == 1)
                          {
                              print("SUCCESS....")
                              if (json["CartID"] as? Int?) != nil
                              {
                                  DispatchQueue.main.async(execute: {
  
                                      print("INSIDE CATEGORIES")
                                      self.addtocartdata.append(Addtocartmodel(json:CartID))
  
  
                                  })
                              }
  
                          }
  
                      }
                  })
  
              }
          })
  
          dataTask.resume()
      }
  }
  </code></pre>
  
  <p>last problem in append the values :</p>
  
  <p><a href="https://i.stack.imgur.com/lEk79.png" rel="noreferrer"><img src="https://i.stack.imgur.com/lEk79.png" alt="enter image description here"></a></p>
  
  <p>In my model data class is look like this  :</p>
  
  <pre><code>class Addtocartmodel
  
  {
   var cartid : Int?
  init(json:NSDictionary)
      {
           self.cartid = json["CartID"] as? Int
  }
  }
  </code></pre>