简体   繁体   中英

JsonSerializer.Deserialize exception: The JSON value could not be converted to System.String

I'm trying to read this json string players.Metadata :

{"drunk":0,"isbleeding":false,"stress":5,"licences":{"drive_boat":false,"hunting":false,"business":false,"driver_bike":false,"dmv":true,"weapon":false,"drive_truck":false,"drive_fly":false,"driver":true},"tracker":false,"craftingrep":0,"commandbinds":[],"phonedata":{"SerialNumber":86505294,"InstalledApps":[]},"bloodtype":"B+","status":[],"walletid":"QB-40404499","poop":0,"isdead":false,"fingerprint":"NQ795L08aVN5152","jobrep":{"taxi":0,"trucker":0,"hotdog":0,"tow":0},"callsign":"NO CALLSIGN","armor":100,"hunger":83.19999999999709,"criminalrecord":{"hasRecord":false},"inside":{"apartment":[]},"inlaststand":false,"ishandcuffed":false,"fitbit":[],"attachmentcraftingrep":0,"inpdjail":0,"beard":0.0,"injail":0,"currentapartment":"apartment56922","jailitems":[],"phone":[],"clean":98.0,"dealerrep":0,"drug":0,"thirst":84.79999999999927}

This is how it looks with json visualizer :

在此处输入图像描述

This is the code I'm using:

Metadata.cs (only including field related to error as other fields are working)

public class Metadata
{
    /* All other fields are working or didn't step into them */
    public string fitbit { get; set; }        
}

Main.cs

Metadata metadata = new Metadata();
metadata = JsonSerializer.Deserialize<Metadata>(players.Metadata);

Error msg: System.Text.Json.JsonException: 'The JSON value could not be converted to System.String. Path: $.fitbit | LineNumber: 0 | BytePositionInLine: 650.'

How can I convert it correctly?

Because fitbit is an empty array. It is not a string.

Your json is invalid, one of the property fitbit has string value, another is shown as an empty array. Change your Metadata class

public class Metadata
{
   public object fitbit { get; set; }       
}

or fix your json

"fitbit" : "",

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM