Ubhaya Kalanamiththa
Ubhaya Kalanamiththa

Reputation: 63

Deserialize of JSON string containg key,value pair and some values without keys

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,"","","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": [] }

this is the JSON string that i tried to deserialize. this contain key,value pairs(

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

) and some part contain only values without keys(

"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"]]

).

i tried to used Newston JSON Converter. here is my code

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; }
}

when i run this code i'm getting this error code

Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'torrents' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

what is the way to fix this error in c#?

Upvotes: 0

Views: 307

Answers (1)

Pikoh
Pikoh

Reputation: 7713

Your JSON string doesn't validate. That's because the path strings you have. You have to use double backslashes there, so this would be a valid 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": []
}  

Notice the path strings "C:\\Users\\Ubhaya Kalanamiththa\\Downloads"

This would be the class you'll need to deserialize it using 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; }
}

Upvotes: 3

Related Questions