Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
149 views
Welcome To Ask or Share your Answers For Others

1 Answer

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...