ich diesen Fehler erhalte, wenn ich versuche unter dem JSON-String deserialisieren:Zusätzliche Informationen: Fehler beim Konvertieren Wert [string] eingeben [Enumeration]
Fehler:
Additional information: Error converting value "invalid_request_error" to type 'ErrorType'. Path 'type', line 2, position 33.
JSON-String
{
"error": {
"type": "invalid_request_error",
"message": "Invalid request (check that your POST content type is application/x-www-form-urlencoded). If you have any questions, we can help at https://support.stripe.com/."
}
}
-Code
private void btnDeserialize_Click(object sender, EventArgs e)
{
var r = Components.JsonMapper.MapFromJson<Components.DTO.Error>(txtToDeserialize.Text, "error");
txtDeserialized.Text = JsonConvert.SerializeObject(r);
}
JsonMapper
public static class JsonMapper
{
public static T MapFromJson<T>(string json, string parentToken = null)
{
var jsonToParse = string.IsNullOrEmpty(parentToken) ? json : JObject.Parse(json).SelectToken(parentToken).ToString();
return JsonConvert.DeserializeObject<T>(jsonToParse);
}
}
Enum
public enum ErrorCode
{
Default,
[JsonProperty("invalid_number")]
InvalidNumber,
[JsonProperty("invalid_expiry_month")]
InvalidExpiryMonth,
[JsonProperty("invalid_expiry_year")]
InvalidExpiryYear,
[JsonProperty("invalid_cvc")]
InvalidCvc,
[JsonProperty("incorrect_number")]
IncorrectNumber,
[JsonProperty("expired_card")]
ExpiredCard,
[JsonProperty("incorrect_cvc")]
IncorrectCvc,
[JsonProperty("incorrect_zip")]
IncorrectZip,
[JsonProperty("card_declined")]
CardDeclined,
[JsonProperty("missing")]
Missing,
[JsonProperty("processing_error")]
ProcessingError
}
public enum ErrorType
{
Default,
[JsonProperty("api_connection_error")]
ApiConnectionError,
[JsonProperty("api_error")]
ApiError,
[JsonProperty("authentication_error")]
AuthenticationError,
[JsonProperty("card_error")]
CardError,
[JsonProperty("invalid_request_error")]
InvalidRequestError,
[JsonProperty("rate_limit_error")]
RateLimitError
}
Ich möchte als Zeichenfolge zur Verwendung von Aufzählungen eher bleiben.
Was könnte der gute Workaround dafür sein?
Sie meinen Tag retten! Vielen Dank! – fiberOptics