如何将以数字开头的 json 对象转换为 C#?

how to convert json object which is starting with digit to c#?

如何将以数字开头的json对象转换为c#?

我的模型是:

    public class TransactionPaymentResponse
    {
         [JsonPropertyName("3DSecure")]
        public _3DSecure _3DSecure { get; set; }

       // removed rest
    }

    public class _3DSecure
    {
        [JsonPropertyName("status")]
        public string Status { get; set; }
    }

反序列化

 var responseStream = await httpResponseMessageForTranscation.Content.ReadAsStringAsync();
            responseTranscation = Newtonsoft.Json.JsonConvert.DeserializeObject<TransactionPaymentResponse>(responseStream);
return responseTranscation;

这是我的 json 字符串(http post 请求的结果)

"{\"statusCode\":\"2001\",\"statusDetail\":\"Transaction rejected.\",\"transactionId\":\"3D941962-A0FE-37ED-503A-BBEE4C6EE535\",\"transactionType\":\"Payment\",\"retrievalReference\":0,\"paymentMethod\":{\"card\":{\"cardType\":\"Visa\",\"lastFourDigits\":\"0006\",\"expiryDate\":\"0223\",\"cardIdentifier\":\"2DEDC06B-6F96-4C78-8E64-9F1CD2107A8F\",\"reusable\":false}},\"amount\":{\"totalAmount\":50,\"saleAmount\":50,\"surchargeAmount\":0},\"currency\":\"GBP\",\"fiRecipient\":{},\"status\":\"Rejected\",\"avsCvcCheck\":{\"status\":\"NoMatches\",\"address\":\"NotProvided\",\"postalCode\":\"NotProvided\",\"securityCode\":\"NotProvided\"},\"3DSecure\":{\"status\":\"NotAuthenticated\"}}"

但是 responseTranscation 的输出是:

{
    "statusCode": "2001",
    "statusDetail": "Transaction rejected ",
    "transactionId": "3D941962-A0FE-37ED-503A-BBEE4C6EE535",
    "transactionType": "Payment",
    "retrievalReference": 0,
    "3DSecure": null   
   // reomved rest of the output
   }

我的 _3DSecure 模型有什么问题?

在 json 字符串中,值为 \"3DSecure\":{\"status\":\"NotAuthenticated\" 但在我看来 3DSecure: null

为什么?任何人都请帮助我

JsonPropertyName 属性与 System.Text.Json 序列化程序一起使用。

对于 Newtonsoft.Json,您需要使用 JsonProperty 属性。

public class TransactionPaymentResponse
{
    [JsonProperty("3DSecure")]
    public _3DSecure _3DSecure { get; set; }

   // removed rest
}

public class _3DSecure
{
    [JsonProperty("status")]
    public string Status { get; set; }
}

完成这些更改后,您的示例 JSON 可以正确反序列化。