-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
System.Text.Json :The JSON value could not be converted to System.String #56760
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue Detailsthe json such as
var model=JsonSerializer.Deserialize(Data.DecryptByPrivateKey(priviteKey), new JsonSerializerOptions { PropertyNameCaseInsensitive = true }) ----error System.Text.Json.JsonException:“The JSON value could not be converted to System.String. Path: $.payid | LineNumber: 0 | BytePositionInLine: 149
|
Aside from not knowing what Data.DecryptByPrivateKey(priviteKey) returns in this context, JSON is still invalid for
as you have PayId declared as string in this class but you have payid to be number in JSON. |
@ryank425 |
I think the issue is still the same. Your payid JSON contains a numeric value, 8752593, which is not enclosed with double quotes like payStatus, but your class definition contains PayId as string. Either making the JSON value to hold the string representation ("8752593"), or changing the following line doesn't produce any more exceptions. From |
@ryank425 |
Assuming string PayID's still use numeric representation you might want to consider doing the following: public class PayNotifyModel
{
public int PayId { get; set; }
} then using the following configuration: var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
NumberHandling = JsonNumberHandling.AllowReadingFromString
};
var result = JsonSerializer.Deserialize<PayNotifyModel>(json, options); deserialization should work for both |
Closing; quoted strings are opt-in in STJ |
the json such as
"{\"payid\":8752593,\"payPreferential\":0,\"payStatus\":\"1\"}"
var model=JsonSerializer.Deserialize<PayNotifyModel>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }) --error System.Text.Json.JsonException:“The JSON value could not be converted to System.String. Path: $.payid | LineNumber: 0 | BytePositionInLine: 149
The text was updated successfully, but these errors were encountered: