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

时间:2016-10-10 11:07:35

标签: c# json

  

string strJson = {" 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 """""成品   100.0%"," 1",1475885736,1475886039,""," C:\ Users \ Ubhaya Kalanamiththa \ Downloads",0,&# 34; 57FA52E7&#34],       [" D9CD68E5AC219D574C1BA2714EF32F6C9BC14AB6",136," The Green Hornet(2011)",787687134,164,129400832,11829248,91,0,0,0,"&#34 ;,0,0,0,0,10545,1,658286302,"""""停止   16.4%"," 5",1475985070,0,""," C:\ Users \ Ubhaya Kalanamiththa \ Downloads \ The Green Hornet(2011)& #34;,0," E156BE18"]],       "标签":[]," torrentc":" 928729876"       ," rssfeeds":[]       ," rssfilters":[]       }

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

  

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

)并且某些部分仅包含没有键的值(

  

"种子:[[" 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.0%"," 1",1475885736,1475886039,""," C:\ Users \ Ubhaya Kalanamiththa \ Downloads",0,&# 34; 57FA52E7"],[" D9CD68E5AC219D574C1BA2714EF32F6C9BC14AB6" 136"该   青蜂侠   (2011)",787687134,164,129400832,11829248,91,0,0,0,"",0,0,0,0,10545,1,658286302,&#34 ;""""停止   16.4%"," 5",1475985070,0,""," C:\ Users \ Ubhaya Kalanamiththa \ Downloads \ The Green Hornet(2011)& #34;,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])反序列化为类型&#39;种子&#39;因为该类型需要一个JSON对象(例如{&#34; name&#34;:&#34; value&#34;})才能正确反序列化。

在c#中修复此错误的方法是什么?

1 个答案:

答案 0 :(得分: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; }
}