使用NewtonSoft.JSON将JSON转换为数组会导致错误 [英] Converting JSON to Array using NewtonSoft.JSON results in Error
本文介绍了使用NewtonSoft.JSON将JSON转换为数组会导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将以下JSON转换为数组
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.
推荐答案
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();
这篇关于使用NewtonSoft.JSON将JSON转换为数组会导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文