C# 使用NewtonSoft.JSON将JSON转换为数组会导致错误
我正在尝试将以下JSON转换为数组C# 使用NewtonSoft.JSON将JSON转换为数组会导致错误,c#,.net,json,json.net,C#,.net,Json,Json.net,我正在尝试将以下JSON转换为数组 {"Lavasoft":null,"STOPzilla":null,"Zillya":null,"VirusBlokAda":{"scan_time":0,"def_time":"2020-07-10T15:03:00Z","scan_result_i":1,"threat_found&quo
{"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}
您无法自动将json反序列化为
scannerinfo[]
,因为json只包含对象,因此您可以将其反序列化为Dictionary
,并将结果转换为scannerinfo[]
:
scannerinfo[] reslt = JsonConvert.DeserializeObject<Dictionary<string, scannerinfo>>(json)
.Values
.Where(x => x != null)
.ToArray();
scannerinfo[]reslt=JsonConvert.DeserializeObject(json)
价值观
.其中(x=>x!=null)
.ToArray();
您可以使用Linq+提取信息。它会将扫描仪名称写入name
属性,您需要将此属性添加到您的模型中
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();
var jObject=jObject.Parse(json);
var infos=jObject.SelectTokens($…扫描时间)
.Select(n=>n.Parent.Parent)
.选择(n=>
{
var info=n.ToObject();
info.Name=n.Path;
退货信息;
})
.ToList();
谢谢。。。是否有办法在
scannerinfo
scannerinfo[] reslt = JsonConvert.DeserializeObject<Dictionary<string, scannerinfo>>(json)
.Values
.Where(x => x != null)
.ToArray();
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();