Skip to content
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

Closed
quan0zhou opened this issue Aug 3, 2021 · 7 comments
Closed
Assignees
Labels
area-System.Text.Json question Answer questions and provide assistance, not an issue with source code or documentation.
Milestone

Comments

@quan0zhou
Copy link

quan0zhou commented Aug 3, 2021

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

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Aug 3, 2021
@ghost
Copy link

ghost commented Aug 3, 2021

Tagging subscribers to this area: @eiriktsarpalis, @layomia
See info in area-owners.md if you want to be subscribed.

Issue Details

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(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
`

Author: quan0zhou
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@ryank425
Copy link

ryank425 commented 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


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.

@quan0zhou
Copy link
Author

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

@ryank425
Copy link

ryank425 commented Aug 3, 2021

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

@quan0zhou
Copy link
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

@eiriktsarpalis
Copy link
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.

@eiriktsarpalis eiriktsarpalis added this to the 6.0.0 milestone Aug 3, 2021
@eiriktsarpalis eiriktsarpalis added customer assistance and removed untriaged New issue has not been triaged by the area owner labels Aug 3, 2021
@eiriktsarpalis eiriktsarpalis self-assigned this Aug 4, 2021
@steveharter
Copy link
Member

Closing; quoted strings are opt-in in STJ

@ghost ghost locked as resolved and limited conversation to collaborators Sep 3, 2021
@eiriktsarpalis eiriktsarpalis added question Answer questions and provide assistance, not an issue with source code or documentation. and removed customer assistance labels Oct 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

4 participants