谷歌支付解析JSON令牌

时间:2018-07-23 01:41:50

标签: c# json visual-studio

当尝试解析Google支付令牌时,我在Newtonsoft.Json.dll中得到了'Newtonsoft.Json.JsonReaderException'异常,(来自https://developers.google.com/pay/api/android/guides/resources/payment-data-cryptography#using-tink的示例令牌)

{
    "protocolVersion":"ECv1",
    "signature":"TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ",
    "signedMessage":" {\"encryptedMessage\":\"ZW5jcnlwdGVkTWVzc2FnZQ==\",
    \"ephemeralPublicKey\":\"ZXBoZW1lcmFsUHVibGljS2V5\",
    \"tag\":\"c2lnbmF0dXJl\"}"
} 
using JSONObject.Parse()

after removing special characters i end up with the following 

 {"protocolVersion":"ECv1",
 "signature":"TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ",
 "signedMessage":" 
 {"encryptedMessage":"ZW5jcnlwdGVkTWVzc2FnZQ==",
 "ephemeralPublicKey":"ZXBoZW1lcmFsUHVibGljS2V5","tag":"c2lnbmF0dXJl"}"}

 The exception is as followed: Exception thrown: 'Newtonsoft.Json.JsonReaderException' in Newtonsoft.Json.dll

 in terms of the class being used, I cannot provide the full code as it is work related, but the following is the code where JSON is used

        googlePayXml = Regex.Replace(googlePayXml, "[^A-Za-z0-9+/=\s]", "")
        googlePayXml = Regex.Replace(googlePayXml, "\\", "")

        'Base64 decode Google Pay PaymentData'
        Dim decodedGooglePayPaymentData As String = 
        Encoding.UTF8.GetString(Convert.FromBase64String(googlePayXml))
        Dim paymentData As JObject = 
        JObject.Parse(decodedGooglePayPaymentData)

有人可以告诉我格式有什么问题吗?

1 个答案:

答案 0 :(得分:0)

就像更清晰地提到的那样,样本令牌的格式不正确,一旦我将括号移到外部,json.parse就可以正常工作