You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
System.Text.Json :The JSON value could not be converted to System.String #56760
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
Aside from not knowing what Data.DecryptByPrivateKey(priviteKey) returns in this context,
JSON "{payid\":8752593,\"payPreferential\":0,\"payStatus\":\"1\"}"
--> "{\"payid\":\"8752593\",\"payPreferential\":0,\"payStatus\":\"1\"}" (after adding " to it to make valid)
is still invalid for
public class PayNotifyModel
{
public string PayId { get; set; }
public string PayStatus { get; set; }
public int PayPreferential { get; set; }
}
as you have PayId declared as string in this class but you have payid to be number in JSON.
@ryank425
Sorry, the code above is unclear, returning JSON is described above. "{\"payid\":8752593,\"payPreferential\":0,\"payStatus\":\"1\"}"
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 public string PayId { get; set; }
To public int PayId { get; set; }
@ryank425
Yes, according to the above modification is no problem, but the customer interface is possible to return to the int type PayID or return the string type PayID,
Now, I use Newtonsoft.json is no problem, it can convert JSON int value to string value
Yes, according to the above modification is no problem, but the customer interface is possible to return to the int type PayID or return the string type PayID
Assuming string PayID's still use numeric representation you might want to consider doing the following:
Activity
ryank425 commentedon Aug 3, 2021
Aside from not knowing what Data.DecryptByPrivateKey(priviteKey) returns in this context,
JSON
"{payid\":8752593,\"payPreferential\":0,\"payStatus\":\"1\"}"-->
"{\"payid\":\"8752593\",\"payPreferential\":0,\"payStatus\":\"1\"}"(after adding " to it to make valid)is still invalid for
as you have PayId declared as string in this class but you have payid to be number in JSON.
quan0zhou commentedon Aug 3, 2021
@ryank425
Sorry, the code above is unclear, returning JSON is described above.
"{\"payid\":8752593,\"payPreferential\":0,\"payStatus\":\"1\"}"ryank425 commentedon Aug 3, 2021
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
public string PayId { get; set; }To
public int PayId { get; set; }quan0zhou commentedon Aug 3, 2021
@ryank425
Yes, according to the above modification is no problem, but the customer interface is possible to return to the int type PayID or return the string type PayID,
Now, I use
Newtonsoft.jsonis no problem, it can convert JSON int value to string valueeiriktsarpalis commentedon Aug 3, 2021
Assuming string PayID's still use numeric representation you might want to consider doing the following:
then using the following configuration:
deserialization should work for both
{"payid":8752593}and{"payid":"8752593"}representations.steveharter commentedon Aug 4, 2021
Closing; quoted strings are opt-in in STJ