Converting JSON to Array using NewtonSoft.JSON results in Error

I’m trying to convert the following JSON to an array

{"Lavasoft":null,"STOPzilla":null,"Zillya":null,"VirusBlokAda":{"scan_time":0,"def_time":"2020-07-10T15:03:00Z","scan_result_i":1,"threat_found":"EICAR-Test-File"},"TrendMicro":{"scan_time":442,"def_time":"2020-07-10T20:22:00Z","scan_result_i":1,"threat_found":"Eicar_test_file"},"SUPERAntiSpyware":{"scan_time":846,"def_time":"2020-07-09T19:18:00Z","scan_result_i":1,"threat_found":"NotAThreat.EICAR[TestFile]"},"nProtect":null,"NANOAV":{"scan_time":2,"def_time":"2020-07-10T22:28:00Z","scan_result_i":1,"threat_found":"Marker.Dos.EICAR-Test-File.dyb"},"Fsecure":null,"ESET":{"scan_time":0,"def_time":"2020-07-11T00:00:00Z","scan_result_i":1,"threat_found":"Eicar test file"},"BitDefender":{"scan_time":7,"def_time":"2020-07-11T10:53:00Z","scan_result_i":1,"threat_found":"EICAR-Test-File (not a virus)"},"Baidu":null,"Ahnlab":{"scan_time":0,"def_time":"2020-07-11T13:57:00Z","scan_result_i":1,"threat_found":"Virus/EICAR_Test_File"},"AegisLab":{"scan_time":0,"def_time":"2020-07-11T07:57:00Z","scan_result_i":1,"threat_found":"Test.File.EICAR.00x7"},"Zoner":null,"ThreatTrack":null,"Sophos":{"scan_time":2,"def_time":"2020-07-11T04:42:00Z","scan_result_i":1,"threat_found":"EICAR-AV-Test"},"Preventon":{"scan_time":40,"def_time":"2020-07-11T10:21:00Z","scan_result_i":1,"threat_found":"EICAR-AV-Test"},"McAfee":{"scan_time":5,"def_time":"2020-07-11T00:00:00Z","scan_result_i":1,"threat_found":"EICAR test file"},"K7":{"scan_time":0,"def_time":"2020-07-11T00:28:00Z","scan_result_i":1,"threat_found":"EICAR_Test_File"},"Jiangmin":{"scan_time":679,"def_time":"2020-07-08T19:18:00Z","scan_result_i":1,"threat_found":"EICAR-Test-File"},"Hauri":{"scan_time":0,"def_time":"2020-07-11T00:00:00Z","scan_result_i":1,"threat_found":"EICAR-test"},"Fprot":null,"Fortinet":{"scan_time":11,"def_time":"2020-07-10T00:00:00Z","scan_result_i":1,"threat_found":"EICAR_TEST_FILE"},"Filseclab":{"scan_time":161,"def_time":"2020-06-22T00:09:00Z","scan_result_i":1,"threat_found":"EICAR.Test.File.zewa"},"Emsisoft":{"scan_time":16,"def_time":"2020-07-10T23:53:00Z","scan_result_i":1,"threat_found":"EICAR-Test-File (not a virus) (B)"},"ClamAV":{"scan_time":14,"def_time":"2020-07-10T14:01:00Z","scan_result_i":1,"threat_found":"Win.Test.EICAR_HDB-1"},"ByteHero":{"scan_time":195,"def_time":"2020-07-09T00:00:00Z","scan_result_i":0,"threat_found":""},"Avira":{"scan_time":0,"def_time":"2020-07-11T00:00:00Z","scan_result_i":1,"threat_found":"Eicar-Test-Signature"},"AVG":null,"Agnitum":null,"Ikarus":{"scan_time":0,"def_time":"2020-07-11T12:19:04Z","scan_result_i":1,"threat_found":"EICAR-Test-File"},"Cyren":{"scan_time":6,"def_time":"2020-07-11T13:54:00Z","scan_result_i":1,"threat_found":"EICAR_Test_File"},"MicrosoftSecurityEssentials":null,"QuickHeal":null,"TotalDefense":null,"TrendMicroHouseCall":null,"XvirusPersonalGuard":null,"DrWebGateway":null,"VirITeXplorer":null} 

scannerinfo[] scanners = JsonConvert.DeserializeObject<scannerinfo[]>(x);  public class scannerinfo     {         public int scan_time { get; set; }         public DateTime def_time { get; set; }         public int scan_result_i { get; set; }         public string threat_found { get; set; }      } 

But i keep getting the following exception

 Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'ScanInfo+scannerinfo[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. 
Add Comment
2 Answer(s)

You can use Linq + JSON Queries to extract the info. It will write the scanner name to Name property, you will need to add this property to your model.

var jObject = JObject.Parse(json); var infos = jObject.SelectTokens("$..scan_time")     .Select(n => n.Parent.Parent)     .Select(n =>     {         var info = n.ToObject<scannerinfo>();         info.Name = n.Path;         return info;     })     .ToList(); 
Answered on July 16, 2020.
Add Comment

You can’t Deserialize the json to scannerinfo[] automatically, because the json contains just objects, so you could deserialize it to Dictionary<string, scannerinfo> and convert the result to scannerinfo[] :

scannerinfo[] reslt = JsonConvert.DeserializeObject<Dictionary<string, scannerinfo>>(json)     .Values     .Where(x => x != null)     .ToArray(); 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.