2015-02-24 47 views
0

我需要解析vb.net中的以下json。我使用的是Json.net,但不知道如何去做。vb.net中的Json解析问题

扫描结果中出现的问题包含扫描详细信息,包含反病毒名称的一些细节。但所有的反病毒都是对象而不是数组。所以请任何机构告诉我该怎么做。

{"file_id":"aaa60a443e3a4426944da9e6fe8a3f3c","scan_results":{"scan_details":{"AegisLab":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-23T00:00:00Z","scan_time":1.0},"Agnitum":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-22T00:00:00Z","scan_time":1.0},"Ahnlab":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-24T00:00:00Z","scan_time":1.0},"Antiy":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-23T00:00:00Z","scan_time":1.0},"AVG":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-23T00:00:00Z","scan_time":1.0},"Avira":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-23T00:00:00Z","scan_time":1.0},"BitDefender":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-23T00:00:00Z","scan_time":1.0},"ByteHero":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-23T00:00:00Z","scan_time":1.0},"ClamWin":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-23T00:00:00Z","scan_time":1.0},"CYREN":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-23T00:00:00Z","scan_time":1.0},"DrWebGateway":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-23T00:00:00Z","scan_time":1.0},"Emsisoft":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-23T00:00:00Z","scan_time":1.0},"ESET":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-23T00:00:00Z","scan_time":1.0},"Filseclab":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-23T00:00:00Z","scan_time":1.0},"Fortinet":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-23T00:00:00Z","scan_time":1.0},"F-prot":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-23T00:00:00Z","scan_time":1.0},"F-secure":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-22T00:00:00Z","scan_time":1.0},"GFI":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-23T00:00:00Z","scan_time":1.0},"Hauri":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-23T00:00:00Z","scan_time":1.0},"Ikarus":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-22T00:00:00Z","scan_time":1.0},"Jiangmin":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-22T00:00:00Z","scan_time":1.0},"K7":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-20T00:00:00Z","scan_time":1.0},"Kaspersky":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-22T00:00:00Z","scan_time":1.0},"Lavasoft":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-22T00:00:00Z","scan_time":1.0},"McAfee-Gateway":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-23T00:00:00Z","scan_time":1.0},"Microsoft":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-22T00:00:00Z","scan_time":1.0},"NANO":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-22T00:00:00Z","scan_time":1.0},"nProtect":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-23T00:00:00Z","scan_time":1.0},"Preventon":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-22T00:00:00Z","scan_time":1.0},"QuickHeal":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-22T00:00:00Z","scan_time":1.0},"Sophos":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-22T00:00:00Z","scan_time":1.0},"STOPzilla":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-21T00:00:00Z","scan_time":1.0},"SUPERAntiSpyware":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-22T00:00:00Z","scan_time":1.0},"Symantec":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-22T00:00:00Z","scan_time":1.0},"Tencent":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-23T00:00:00Z","scan_time":1.0},"TotalDefense":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-22T00:00:00Z","scan_time":1.0},"TrendMicro":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-21T00:00:00Z","scan_time":1.0},"TrendMicroHouseCall":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-21T00:00:00Z","scan_time":1.0},"VirIT":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-20T00:00:00Z","scan_time":1.0},"VirusBlokAda":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-20T00:00:00Z","scan_time":1.0},"Xvirus":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-23T00:00:00Z","scan_time":1.0},"Zillya!":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-22T00:00:00Z","scan_time":1.0},"Zoner":{"scan_result_i":0,"threat_found":"","def_time":"2015-02-18T00:00:00Z","scan_time":1.0}},"rescan_available":true,"data_id":"32fe182492834b6f88b1d95f6a14c886","scan_all_result_i":0,"start_time":"2015-02-23T13:10:55.549Z","total_time":1.0,"total_avs":43,"progress_percentage":100,"in_queue":0,"scan_all_result_a":"Clean"},"file_info":{"file_size":0,"upload_timestamp":"2015-02-23T00:40:41.029Z","md5":"D41D8CD98F00B204E9800998ECF8427E","sha1":"DA39A3EE5E6B4B0D3255BFEF95601890AFD80709","sha256":"E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855","file_type_category":"O","file_type_description":"empty","file_type_extension":"-","display_name":"C:\\testy.xml"},"data_id":"32fe182492834b6f88b1d95f6a14c886"} 
+0

您用来反序列化json字符串的类和您用于实际反序列化的代码的外观如何? – sloth 2015-02-24 10:02:54

+0

懒惰,我没有做任何事情。其实我昨天开始在vb.net上工作。所以需要一个想法如何做到这一点? – 2015-02-24 10:16:00

回答

1

首先,您需要创建一个可以表示要反序列化的数据的类(或多个)。

看着你所提供的输入字符串,你需要一个类FileInfoScanDetailScanResults和一个根(姑且称之为Scan)。

这只是将JSON键映射到类属性(和ScanDetail的字典,因为您可能不需要每个属性)。

所以,你的类应该是这样的:

public class Scan 
    public property file_id() as string 
    public property scan_results() AS ScanResults 
    public property file_info() as FileInfo 
    public property data_id() as string 
End Class 

public class ScanResults 
    public property scan_details As Dictionary(Of string, ScanDetail) 
    Public Property rescan_available() As Boolean 
    Public Property data_id() As String 
    Public Property scan_all_result_i() As Integer 
    Public Property start_time() As String 
    Public Property total_time() As Double 
    Public Property total_avs() As Integer 
    Public Property progress_percentage() As Integer 
    Public Property in_queue() As Integer 
    Public Property scan_all_result_a() As String 
End Class 

public class ScanDetail 
    public property scan_result_i() As integer 
    public property threat_found() as string 
    public property def_time() as string 
    public property scan_time() as string 
End class 

Public Class FileInfo 
    Public Property file_size() As Integer 
    Public Property upload_timestamp() As String 
    Public Property md5() As String 
    Public Property sha1() As String 
    Public Property sha256() As String 
    Public Property file_type_category() As String 
    Public Property file_type_description() As String 
    Public Property file_type_extension() As String 
    Public Property display_name() As String 
End Class 

现在反序列化是那么容易,因为

Dim details = NewtonSoft.JSon.JsonConvert.DeserializeObject(Of Scan)(your_json_string) 

要获得所有threat_found值,可以方便的查询结果:

Dim threats = details.scan_results.scan_details.Select(Function(kvp) kvp.Value.threat_found) _ 
               .Where(Function(t) Not String.IsNullOrWhiteSpace(t)) _ 
               .ToList() 
+0

我需要在特定文件上获取病毒计数。所以从你的代码中,我认为我无法做到 – 2015-02-24 11:11:43

+0

@ user1679960你的JSON字符串中没有'Virus Count'。 – sloth 2015-02-24 11:19:10

+0

@ user1679960看来你正在寻找'threat_found'。我更新了我的答案。 – sloth 2015-02-24 13:31:00

0

这里是如何反序列化JSON字符串,假设你引用了Json.Net。你得到的是一个动态对象,其属性名称与JSON属性名称相同。如果您要创建一个具有正确属性的类,则还可以反序列化为该特定类型,并且您将拥有强大的打字功能。在此代码之后放置一个断点并使用Debug运行。看看本地人窗口,你可以检查“theobj”的属性。

Dim obj As String = "{""file_id"":""aaa60a443e3a4426944da9e6fe8a3f3c"",""scan_results"":{""scan_details"":{""AegisLab"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-23T00:00:00Z"",""scan_time"":1.0},""Agnitum"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-22T00:00:00Z"",""scan_time"":1.0},""Ahnlab"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-24T00:00:00Z"",""scan_time"":1.0},""Antiy"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-23T00:00:00Z"",""scan_time"":1.0},""AVG"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-23T00:00:00Z"",""scan_time"":1.0},""Avira"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-23T00:00:00Z"",""scan_time"":1.0},""BitDefender"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-23T00:00:00Z"",""scan_time"":1.0},""ByteHero"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-23T00:00:00Z"",""scan_time"":1.0},""ClamWin"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-23T00:00:00Z"",""scan_time"":1.0},""CYREN"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-23T00:00:00Z"",""scan_time"":1.0},""DrWebGateway"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-23T00:00:00Z"",""scan_time"":1.0},""Emsisoft"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-23T00:00:00Z"",""scan_time"":1.0},""ESET"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-23T00:00:00Z"",""scan_time"":1.0},""Filseclab"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-23T00:00:00Z"",""scan_time"":1.0},""Fortinet"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-23T00:00:00Z"",""scan_time"":1.0},""F-prot"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-23T00:00:00Z"",""scan_time"":1.0},""F-secure"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-22T00:00:00Z"",""scan_time"":1.0},""GFI"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-23T00:00:00Z"",""scan_time"":1.0},""Hauri"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-23T00:00:00Z"",""scan_time"":1.0},""Ikarus"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-22T00:00:00Z"",""scan_time"":1.0},""Jiangmin"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-22T00:00:00Z"",""scan_time"":1.0},""K7"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-20T00:00:00Z"",""scan_time"":1.0},""Kaspersky"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-22T00:00:00Z"",""scan_time"":1.0},""Lavasoft"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-22T00:00:00Z"",""scan_time"":1.0},""McAfee-Gateway"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-23T00:00:00Z"",""scan_time"":1.0},""Microsoft"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-22T00:00:00Z"",""scan_time"":1.0},""NANO"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-22T00:00:00Z"",""scan_time"":1.0},""nProtect"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-23T00:00:00Z"",""scan_time"":1.0},""Preventon"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-22T00:00:00Z"",""scan_time"":1.0},""QuickHeal"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-22T00:00:00Z"",""scan_time"":1.0},""Sophos"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-22T00:00:00Z"",""scan_time"":1.0},""STOPzilla"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-21T00:00:00Z"",""scan_time"":1.0},""SUPERAntiSpyware"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-22T00:00:00Z"",""scan_time"":1.0},""Symantec"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-22T00:00:00Z"",""scan_time"":1.0},""Tencent"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-23T00:00:00Z"",""scan_time"":1.0},""TotalDefense"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-22T00:00:00Z"",""scan_time"":1.0},""TrendMicro"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-21T00:00:00Z"",""scan_time"":1.0},""TrendMicroHouseCall"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-21T00:00:00Z"",""scan_time"":1.0},""VirIT"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-20T00:00:00Z"",""scan_time"":1.0},""VirusBlokAda"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-20T00:00:00Z"",""scan_time"":1.0},""Xvirus"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-23T00:00:00Z"",""scan_time"":1.0},""Zillya!"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-22T00:00:00Z"",""scan_time"":1.0},""Zoner"":{""scan_result_i"":0,""threat_found"":"""",""def_time"":""2015-02-18T00:00:00Z"",""scan_time"":1.0}},""rescan_available"":true,""data_id"":""32fe182492834b6f88b1d95f6a14c886"",""scan_all_result_i"":0,""start_time"":""2015-02-23T13:10:55.549Z"",""total_time"":1.0,""total_avs"":43,""progress_percentage"":100,""in_queue"":0,""scan_all_result_a"":""Clean""},""file_info"":{""file_size"":0,""upload_timestamp"":""2015-02-23T00:40:41.029Z"",""md5"":""D41D8CD98F00B204E9800998ECF8427E"",""sha1"":""DA39A3EE5E6B4B0D3255BFEF95601890AFD80709"",""sha256"":""E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855"",""file_type_category"":""O"",""file_type_description"":""empty"",""file_type_extension"":""-"",""display_name"":""C:\\testy.xml""},""data_id"":""32fe182492834b6f88b1d95f6a14c886""}" 
    Dim theobj = JsonConvert.DeserializeObject(obj) 
+0

我需要迭代json。其实我需要在特定文件上获得病毒计数。所以从你的代码我想我无法做到 – 2015-02-24 11:11:00

+0

不,你可以。你可以得到任何值,但用@懒惰的方法会更容易。或者,您可以安装Web Essentials for Visual Studio,它将“将json粘贴为类”项目添加到编辑菜单中......尽管我不确定它是否执行VB.Net或仅仅是c#。 – Crowcoder 2015-02-24 11:34:40

+0

你能看到我的答案,我已经做到了...但只需要验证是否有任何其他方法在性能方面更好, – 2015-02-24 11:39:08

-1

感谢您的回复家伙。我通过以下代码片段完成了它:

Dim jsonstring = IO.File.ReadAllText("C:\\Users\\Administrator\\Desktop\\json.txt") 
    Dim jo = Newtonsoft.Json.Linq.JObject.Parse(jsonstring) 
    Dim scanDetail = jo("scan_results")("scan_details") 
    Dim virusCount As Integer 
    virusCount = 0 
    For Each entry As Newtonsoft.Json.Linq.JProperty In scanDetail 
     entry.CreateReader() 
     Console.WriteLine(entry.Name) 
     For Each Val As Object In entry 
      Dim abc = Newtonsoft.Json.Linq.JObject.Parse(Val.ToString()) 
      Dim threatFound As String = abc("threat_found") 

      Dim result As Integer = String.Compare(threatFound, "") 
      If result = -1 Then 
       virusCount = virusCount + 1 
      End If 



     Next 
    Next 

    If virusCount = 0 Then 
     Console.WriteLine("No Virus") 
    Else 
     Console.WriteLine("Virus") 
    End If 
+0

你不需要(实际上你不能在VB中)逃避路径中的反斜杠,所以它应该是“C:\ Users \ Administrator \ Desktop \ json.txt”。 (你真的不应该以管理员身份登录,但这是一个不同的论坛。) – 2015-02-24 11:48:47

+0

谢谢安德鲁莫伦,我真的不知道这件事。 – 2015-02-24 11:55:33

+0

如果您认为这是正确的,请标记为正确答案,因为我无法做到。 – 2015-02-24 11:56:23