2016-10-10 41 views
0

串strJson = { “构建”:42606, “种子”:[[ “3C50FB27DB1469EFFD2F7BEAB9997D6425416380”,136“,Westworld.S01E02.720p.HDTV。 x265.ShAaNiG.mkv“,314721982,1000,314721982,12042240,38,0,0,0,”“,0,0,0,0,65536,-1,0,”“,”“,”完成 100%“,”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,“”,“”,“停止 16.4%”,“5” ,“C:\ Users \ Ubhaya Kalanamiththa \ Downloads \ The Green Hornet(2011)”,0,“E156BE18”]], “label”:[],“torrentc”:“928729876”,1475985070,0, , “rssfeeds”:[] “rssfilters”:[] }反序列化的JSON字符串方含键,值对和一些值,而不键

这是JSON字符串我试图反序列化。这包含键,值对(

“构建”:42606 “torrentc”: “928729876”

)和一些部分仅包含值,而无需键(

“种子:[[ “3C50FB27DB1469EFFD2F7BEAB9997D6425416380”,136 “Westworld.S01E02.720p.HDTV.x265.ShAaNiG.mkv”,314721982,1000,314721982,12042240,38,0,0,0, “”,0,0,0, 0,55536,-1,0,“”,“”,“完成 100.0%”,“1”,1475885736,1475886039,“”,“C:\ Users \ Ubhaya Kalanamiththa \ Downloads”,0,“57FA52E7” ],[“D9CD68E5AC219D574C1BA2714EF3 2F6C9BC14AB6“,136,” Green Hornet (2011)“,787687134,164,129400832,11829248,91,0,0,0,”“,0,0,0,0,10545,1,658286302”, “,”“,”停止 16.4%“,”5“,1475985070,0,”“,”C:\ Users \ Ubhaya Kalanamiththa \ Downloads \ The Green Hornet(2011)“,0,”E156BE18“]]

)。

我试着用Newston JSON Converter。 这里是我的代码

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#中的方式?

回答

2

您的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; } 
} 
+0

当使用streamreader类从互联网路径字符串获取json字符串被自动纠正。感谢Pikoh。乌尔班是正确的.. –

相关问题