转换为json对象时出现问题

时间:2017-11-04 07:11:52

标签: ios json swift

我对事情并不熟悉......因此问问题...... :)

我在标签和文本字段中有一些值,我想在API中作为参数传递。但是在将这些值传递给API之前,它们将采用这种格式......

[{"remaining_balance":"0.0","shipping_charges":0, "grand_total":320,"product_id”:”4”}, {"remaining_balance”:”1.0”,”shipping_charges”:200, "grand_total”:456,”product_id”:”5”}]

并且我希望将它们传递给参数。但是我如何以上面给出的格式获取这些值,我无法弄清楚。希望有人能帮忙..

编辑:这就是我的API请求..

func APICallForDetails() {
    let url = "http://myapp/myUrl……”
    let headers = [ "Content-Type":"application/x-www-form-urlencoded"]

    let Parameters =
        [
            "access_token": self.accessToken,
            "seller_id": sellerIdvar,
            "sort": "Ascending_Name",
            “order_data”: allMyData 
            "user_details": myUserDetails
        ]
    Alamofire.request(url, method: .post, parameters: Parameters, encoding: URLEncoding.httpBody, headers: headers)
        .responseJSON { (response) in
            if let httpResponse = response.response {
                print("error \(httpResponse.statusCode)")

                if httpResponse.statusCode == 200 {
                        print(“Something went wrong”)                                
                        }  else if result["success"] as! Int == 1 {
                            print(“SUCCESS!”)
                            }}
Parameters部分中的

参数order_data的值为allMyDataallMyData应包含[{"remaining_balance":"0.0","shipping_charges":0, "grand_total":320,"product_id":"4"}]

EDIT2 此外,参数user_details的值应为{"first_name”:”Pat”,”last_name”:”Rick”,”customer_email_id”:”pat@gmail.com","customer_address":"I have","city":"the Facebook page","mobile_number”:”1234567890”}。此外,该值不在此处的数组中......

3 个答案:

答案 0 :(得分:0)

试试这个:

 let jsonObject: [AnyObject]  = [
        [
             "remaining_balance":"0",
             "shipping_charges": "0", 
             "grand_total": "320", 
             "product_id": "4", 

        ]
    ]

现在jsonObject保存jsonValues,您可以将其传递给参数

答案 1 :(得分:0)

要显示标签和文本字段中输入的文本,您需要按照此处所示传递并根据您的要求更改数据

 let parameters : [String: Any] = ["address":
               [ "region": "\((stateTextfield.text)!)",
                "region_code": "CA",
                "region_id": 12,
                "country_id": "US",
                "company": "\((companyTextField.text)!)",
                "telephone": "\((mobileNumberTextfield.text)!)",
                "postcode": "\((zipcodeTextfield.text)!)",
                "city": "\((cityTextField.text)!)",
                "firstname": "\((firstNameTextfield.text)!)",
                "lastname": "\((lastNameTextfield.text)!)",
                "email": "\((emailIdTextField.text)!)",
                "prefix": "",
                "sameAsBilling": 1,]]

答案 2 :(得分:0)

你需要这样的东西

func APICallForDetails() {
        let url = "http://myapp/myUrl……"
        let headers = [ "Content-Type":"application/x-www-form-urlencoded"]

        let allMyData : [[String:AnyObject]] = [[
            "remaining_balance":"0" as AnyObject,
            "shipping_charges": 0 as AnyObject,
            "grand_total": 320 as AnyObject,
            "product_id": "4" as AnyObject,
            ],[
            "remaining_balance":"1.0" as AnyObject,
            "shipping_charges": 200 as AnyObject,
            "grand_total": 456 as AnyObject,
            "product_id": "5" as AnyObject,
            ]]

        let Parameters =
            [
                "access_token": self.accessToken,
                "seller_id": sellerIdvar,
                "sort": "Ascending_Name",
                "order_data": allMyData
        ]
        Alamofire.request(url, method: .post, parameters: Parameters, encoding: URLEncoding.httpBody, headers: headers)
            .responseJSON { (response) in
                if let httpResponse = response.response {
                    print("error \(httpResponse.statusCode)")

                    if httpResponse.statusCode == 200 {
                        print("Something went wrong")
                    }  else if result["success"] as! Int == 1 {
                        print("SUCCESS!")
                    }}
        }
    }

要改变的小事

  

参数应该是小写的,因为它是一个变量声明,   大写名称应该是类名

更新了您上次提供的数据,希望这可以帮助您

相关问题