对包含键、值对和一些没有键的值的JSON字符串进行反序列化

本文关键字:JSON 字符串 反序列化 包含键 | 更新日期: 2023-09-27 18:12:34

string strJson={"build":42606,"torrents":[["3C50FB27DB1469EFFD2F7BEAB9997D6425416380",136,"Westworld.S01E02.720p.HDTV.x265.ShAaNiG.mkv",314721982100031472198210424240,38,0,0,0,",0,0,0,065536,-1,0,",","已完成100.0%","1",14758857361475886039,","C:''Users''Ubhaya Kalanamiththa''Downloads",0,"57FA52E7"],["D9CD68E5AC219D574C1BA2714EF32F6C9BC14AB6",136,"绿蜂侠(2011(",78768713416412940083211829248,91,0,0,0,",0,0,010545,1658286302,",","16.4%","5",1475985070,0,","C:''Users''UbhayaKalanamiththa''Downloads''The Green Hornet(2011(",0,"E156BE18"]],"label":[],"torrentc":"928729876","rssfeeds":[],"rssfilters":[]}

这是我试图反序列化的JSON字符串。其中包含密钥、值对(

"build":42606,"torrentc":"928729876">

)某些部分只包含没有密钥的值(

"种子:[["3C50FB27DB1469EFFD2F7BEAB9997D6425416380",136,"西部世界.S01E02.720p.HDTV.x265.ShAaNiG.mkv",31472198221000317219821204240,38,0,0,0,",0,0,065536,-1,0,"","完成100.0%","1",14758857361475886039,","C:''Users''Ubhaya Kalanamiththa''Downloads",0,"57FA52E7"],["D9CD68E5AC219D574C1BA2714EF32F6C9BC14AB6",136,"绿蜂侠(2011(",78768713416412940083211829248,91,0,0,0,",0,0,010545,1658286302,"","已停止16.4%","5",1475985070,0,","C:''Users''UbhayaKalanamiththa''Downloads''The Green Hornet(2011(",0,"E156BE18"]]

)。

我尝试使用Newston JSON转换器。这是我的代码

TORRENTLIST list = JsonConvert.DeserializeObject<TORRENTLIST>(strJson);
public class TORRENTLIST
{
    public int build { get; set; }
    public label label { get; set; }
    public List<torrents> torrents { get; set; }
    public string torrents { get; set; }
}
public class torrents
{
    public string HASH { get; set; }
    public int STATUS { get; set; }
    public string NAME { get; set; }
    public int size { get; set; }
    public int PERCENT_PROGRESS { get; set; }
    public int DOWNLOADED { get; set; }
    public int UPLOADED { get; set; }
    public int RATIO { get; set; }
    public int UPLOAD_SPEED { get; set; }
    public int DOWNLOAD_SPEED { get; set; }
    public int ETA { get; set; }
    public string LABEL { get; set; }
    public int PEERS_CONNECTED { get; set; }
    public int PEERS_IN_SWARM { get; set; }
    public int SEEDS_CONNECTED { get; set; }
    public int SEEDS_IN_SWARM { get; set; }
    public int AVAILABILITY { get; set; }
    public int TORRENT_QUEUE_ORDER { get; set; }
    public int REMAINING { get; set; }
}
public class label
{
    public string LABEL { get; set; }
    public int TORRENT_IN_LABEL { get; set; }
}

当我运行这个代码时,我得到了这个错误代码

附加信息:无法将当前JSON数组(例如[1,2,3](反序列化为类型"torrents",因为该类型需要JSON对象(例如{"name":"value"}(才能正确反序列化。

如何修复c中的这个错误?

对包含键、值对和一些没有键的值的JSON字符串进行反序列化

您的JSON字符串无法验证。这是因为你的路径字符串。你必须在那里使用双反斜杠,所以这将是一个有效的JSON:

{
"build": 42606,
"torrents": [
    ["3C50FB27DB1469EFFD2F7BEAB9997D6425416380", 136, "Westworld.S01E02.720p.HDTV.x265.ShAaNiG.mkv", 314721982, 1000, 314721982, 12042240, 38, 0, 0, 0, "", 0, 0, 0, 0, 65536, -1, 0, "", "", "Finished 100.0 %", "1", 1475885736, 1475886039, "", "C:''Users''Ubhaya Kalanamiththa''Downloads", 0, "57FA52E7"],
    ["D9CD68E5AC219D574C1BA2714EF32F6C9BC14AB6", 136, "The Green Hornet (2011)", 787687134, 164, 129400832, 11829248, 91, 0, 0, 0, "", 0, 0, 0, 0, 10545, 1, 658286302, "", "", "Stopped 16.4 %", "5", 1475985070, 0, "", "C:''Users''Ubhaya Kalanamiththa''Downloads''The Green Hornet (2011)", 0, "E156BE18"]
],
"label": [],
"torrentc": "928729876",
"rssfeeds": [],
"rssfilters": []
}  

注意路径字符串"C:''Users''Ubhaya Kalanamiththa''Downloads"

这将是您需要使用JSON.NET:对其进行反序列化的类

public class TORRENTLIST
{
    public int build { get; set; }
    public List<List<object>> torrents { get; set; }
    public List<object> label { get; set; }
    public string torrentc { get; set; }
    public List<object> rssfeeds { get; set; }
    public List<object> rssfilters { get; set; }
}