2015-02-24 45 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