Skip to content

System.Text.Json :The JSON value could not be converted to System.String #56760

Closed

Description

@quan0zhou

the json such as
"{\"payid\":8752593,\"payPreferential\":0,\"payStatus\":\"1\"}"

public class PayNotifyModel
 {
        public string PayId { get; set; }
        
        public string PayStatus { get; set; }

       public int PayPreferential { get; set; }
  }
 

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

Activity

  1. ghost added
    untriaged
    on Aug 3, 2021
  2. ryank425 commented on Aug 3, 2021

    @ryank425

    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.

  3. quan0zhou commented on Aug 3, 2021

    @quan0zhou
    Author

    @ryank425
    Sorry, the code above is unclear, returning JSON is described above.
    "{\"payid\":8752593,\"payPreferential\":0,\"payStatus\":\"1\"}"

  4. ryank425 commented on Aug 3, 2021

    @ryank425

    @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; }

  5. quan0zhou commented on Aug 3, 2021

    @quan0zhou
    Author

    @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

  6. eiriktsarpalis commented on Aug 3, 2021

    @eiriktsarpalis
    Member

    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:

    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 {"payid":8752593} and {"payid":"8752593"} representations.

  7. added this to the 6.0.0 milestone on Aug 3, 2021
  8. added and removed
    untriaged
    on Aug 3, 2021
  9. steveharter commented on Aug 4, 2021

    @steveharter
    Contributor

    Closing; quoted strings are opt-in in STJ

  10. ghost locked as resolved and limited conversation to collaborators on Sep 3, 2021
  11. added
    question
    and removed on Oct 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-System.Text.Jsonquestion

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions