I need to parse following json in vb.net. I'm using Json.net but do not know how to do it.
Problem occurs in scan result contains scan detail that conaints anti virus name with some detail. but all the anti-virus is object not an array. So please any body tell me how to do it.
{"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"}
First, you need to create a class (or multiple) that can represent the data you want to deserialize.
Looking at the input string you provided, you need a class for FileInfo, ScanDetail, ScanResults and one for the root (let's call it Scan).
It's simply a matter of mapping the JSON keys to a class property (and a dictionary for ScanDetail, since you probably don't want a property for each of it).
So, your classes should look like this:
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
Now the deserializing is as easy as
Dim details = NewtonSoft.JSon.JsonConvert.DeserializeObject(Of Scan)(your_json_string)
To get all threat_found values, you can easily query the result:
Dim threats = details.scan_results.scan_details.Select(Function(kvp) kvp.Value.threat_found) _
.Where(Function(t) Not String.IsNullOrWhiteSpace(t)) _
.ToList()
Here is how to deserialize that JSON string assuming you have referenced Json.Net. What you get is a dynamic object with property names the same as the JSON property names. If you were to create a Class with the correct properties you could also deserialize into that specific type and you would have the benefit of strong typing. Put a break point after this code and run with Debug. Look in the Locals window and you can inspect "theobj"'s properties.
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)
Thanks for your reply guys. I have done it by following code snippet
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