在发出POST请求Swift时,JSON节点的顺序发生了变化

时间:2017-11-24 18:38:39

标签: json swift authorize.net

我试图发布以下jSON正文 请求JSON:

  Container _renderImage(GroupMessageController message) => new Container(
      width: 300.0,
      margin: const EdgeInsets.all(5.0),
      decoration: new BoxDecoration(
          borderRadius: const BorderRadius.all(const Radius.circular(25.0)),
          image: new DecorationImage(
              fit: BoxFit.contain,
              image: new NetworkImage(message.mediaUrl.toString()))));

  Container _renderImage2(GroupMessageController message) => new Container(
        width: 300.0,
        child: new Image.network(
          message.mediaUrl.toString(),
          fit: BoxFit.contain,
        ),
      );

    [√] Flutter (on Microsoft Windows [Version 10.0.16299.64], locale en-US, channel alpha)
    • Flutter at c:\sdks\flutter
    • Framework revision e8aa40eddd (5 weeks ago), 2017-10-17 15:42:40 -0700
    • Engine revision 7c4142808c
    • Tools Dart version 1.25.0-dev.11.0

[√] Android toolchain - develop for Android devices (Android SDK 27.0.1)
    • Android SDK at C:\Users\ride4\AppData\Local\Android\sdk
    • Platform android-27, build-tools 27.0.1
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-915-b01)

[√] Android Studio (version 3.0)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-915-b01)

[√] IntelliJ IDEA Community Edition (version 2017.2)
    • Flutter plugin version 19.1
    • Dart plugin version 172.4343.25

[√] Connected devices
    • Android SDK built for x86 • emulator-5554 • android-x86 • Android 7.1.1 (API 25) (emulator)

当我尝试打印(参数)节点更改的顺序时,它看起来像

let parameters =  [
        "createTransactionRequest": [
            "merchantAuthentication": [
                "name": "xxxxxxxx",
                "transactionKey": "xxxxxxxxx"
            ],
            "refId": "123456",
            "transactionRequest": [
                "transactionType": "authCaptureTransaction",
                "amount": "5",
                "payment": [
                    "opaqueData": [
                        "dataDescriptor": desc!,
                        "dataValue": tocken!
                    ]
                ]
            ]
        ]
    ]

我得到了像

这样的回复
["createTransactionRequest":

 ["refId": "123456", 
"transactionRequest":

 ["payment":  ["opaqueData": ["dataDescriptor": "COMMON.ACCEPT.INAPP.PAYMENT", "dataValue": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx="]],

 "transactionType": "authCaptureTransaction",

 "amount": "5"],

 "merchantAuthentication": ["name": "xxxxxxx", "transactionKey": 

"6gvE46G5seZt563w"]
]
]

这真烦人。任何人的帮助都将非常感激。

1 个答案:

答案 0 :(得分:0)

您需要更改数据结构,如下所示:

struct Transaction: Codable {
    let createTransactionRequest: CreateTransactionRequest
}
struct CreateTransactionRequest: Codable {
    let merchantAuthentication: MerchantAuthentication
    let refId: String
    let transactionRequest: TransactionRequest
}
struct MerchantAuthentication: Codable {
    let name: String
    let transactionKey: String
}
struct TransactionRequest: Codable {
    let transactionType: String
    let amount: String
    let payment: Payment
}
struct Payment: Codable {
    let opaqueData: OpaqueData
}
struct OpaqueData: Codable {
    let dataDescriptor: String
    let dataValue: String
}

测试

let json = """
    { "createTransactionRequest":
        { "merchantAuthentication":
            { "name": "YOUR_API_LOGIN_ID",
               "transactionKey": "YOUR_TRANSACTION_KEY"
             },
          "refId": "123456",
          "transactionRequest":
               { "transactionType": "authCaptureTransaction",
                 "amount": "5",
                 "payment":
                   { "opaqueData":
                       { "dataDescriptor": "COMMON.ACCEPT.INAPP.PAYMENT",
                         "dataValue": "PAYMENT_NONCE_GOES_HERE"
                        }
                    }
                }

             }

          }
"""
let jsonData = Data(json.utf8)
do {
    let transaction = try JSONDecoder().decode(Transaction.self, from: jsonData)
    print(transaction)
    // encoding
    let encodedData = try JSONEncoder().encode(transaction)
    print(String(data: encodedData, encoding: .utf8)!)
} catch {
    print(error)
}
  

{ “createTransactionRequest”:{ “merchantAuthentication”:{ “名称”: “YOUR_API_LOGIN_ID”, “transactionKey”: “YOUR_TRANSACTION_KEY”}, “REFID”: “123456”, “transactionRequest”:{ “TRANSACTIONTYPE”:“authCaptureTransaction ”, “量”: “5”, “付款”:{ “opaqueData”:{ “dataValue”: “PAYMENT_NONCE_GOES_HERE”, “dataDescriptor”: “COMMON.ACCEPT.INAPP.PAYMENT”}}}}}