簡體   English   中英

JsonSerializer.Deserialize 異常:JSON 值無法轉換為 System.String

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

我正在嘗試閱讀此 json 字符串player.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}

這是json 可視化工具的外觀:

在此處輸入圖像描述

這是我正在使用的代碼:

Metadata.cs (僅包括與錯誤相關的字段,因為其他字段正在工作)

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

主文件

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

錯誤消息: System.Text.Json.JsonException:'JSON 值無法轉換為 System.String。 路徑:$.fitbit | 行號:0 | BytePositionInLine:650。

我怎樣才能正確轉換它?

因為 fitbit 是一個空數組。 它不是一個字符串。

您的 json 無效,其中一個屬性 fitbit 具有字符串值,另一個顯示為空數組。 更改您的元數據 class

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

或修復您的 json

"fitbit" : "",

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM