簡體   English   中英

使用 NewtonSoft.JSON 將 JSON 轉換為數組會導致錯誤

[英]Converting JSON to Array using NewtonSoft.JSON results in Error

我正在嘗試將以下 JSON 轉換為數組

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

    }

但我不斷收到以下異常

 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.

您可以使用 Linq + JSON 查詢來提取信息。 它將掃描儀名稱寫入Name屬性,您需要將此屬性添加到 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();

您無法自動將 json反序列化為scannerinfo[] ,因為json僅包含對象,因此您可以將其反序列化為Dictionary<string, scannerinfo>並將結果轉換為scannerinfo[]

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM