Check If Valid JSON
Public Shared Function IsValidJSON(ByVal json As String) As Boolean
Try
JsonConvert.DeserializeObject(json)
Return True
Catch ex As Exception
Return False
End Try
End Function
Public Shared Function IsValidJSON(ByVal json As String) As Boolean
Try
JToken.Parse(json)
Return True
Catch ex As Exception
Return False
End Try
End Function
Check If A Key Exists
Public Shared Function KeyExists(ByVal json As String, ByVal key As String) As Boolean
Try
Dim obj As JObject = JObject.Parse(json)
Return obj.ContainsKey(key)
Catch ex As Exception
Return False
End Try
End Function
Get Value Of A Key if Valid JSON and Key Exists
Public Shared Async Function GetValueFromJSONAsync(ByVal json As String, ByVal key As String) As Task(Of String)
Try
Dim obj As JObject = JObject.Parse(json)
Dim prop As JProperty = obj.Property(key)
If prop IsNot Nothing Then
Return prop.Value.ToString()
Else
Return “Key does not exist”
End If
Catch ex As Exception
Return “Invalid JSON”
End Try
End Function
Public Shared Async Function GetValueFromJSONAsync(ByVal json As String, ByVal key As String) As Task(Of String)
Dim Rtn$ = “”
Try
Dim obj As JObject = JObject.Parse(json)
Dim prop As JProperty = obj.Property(key)
If prop IsNot Nothing Then
Rtn = prop.Value.ToString()
End If
Catch ex As Exception
Rtn = “”
End Try
Return Await Task.FromResult(Of String)(Rtn)
End Function
Public Shared Function GetValueFromJSON(ByVal json As String, ByVal key As String) As String
Try
Dim obj As JObject = JObject.Parse(json)
If obj.ContainsKey(key) Then
Return obj(key).ToString()
Else
Return “Key does not exist”
End If
Catch ex As Exception
Return “Invalid JSON”
End Try
End Function
Uses :
Dim json As String = “{ “”name””: “”John””, “”age””: 30, “”city””: “”New York”” }”
Dim result As String = Await JSONHelper.GetValueFromJSONAsync(json, “name”)
Console.WriteLine(result)
Public Shared Function GetValueFromJSON(ByVal json As String, ByVal key As String) As String
Try
Dim obj As JObject = JObject.Parse(json)
Dim prop As JProperty = obj.Property(key)
If prop IsNot Nothing Then
Return prop.Value.ToString()
Else
Return “Key does not exist”
End If
Catch ex As Exception
Return “Invalid JSON”
End Try
End Function
Dim jsonString As String = “{ “”body””:””om sai””, “”type””:””chat””, “”notifyName””:””US””, “”from””:””919407755200@c.us””, “”to””:””919406214451@c.us””}”
Dim jsonObject As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(jsonString)
Dim fromValue As String = jsonObject(“from”).ToString()
Console.WriteLine(fromValue)
Dim json As String = “{ “”name””: “”John””, “”age””: 30, “”city””: “”New York”” }”
Dim obj As Object = JsonConvert.DeserializeObject(json)
Console.WriteLine(“Name: ” & obj(“name”))
Console.WriteLine(“Age: ” & obj(“age”))
Console.WriteLine(“City: ” & obj(“city”))
Strongly Typed
Imports Newtonsoft.Json
Public Class Person
Public Property Name As String
Public Property Age As Integer
Public Property City As String
End Class
Dim json As String = “{ “”name””: “”John””, “”age””: 30, “”city””: “”New York”” }”
Dim person As Person = JsonConvert.DeserializeObject(Of Person)(json)
Console.WriteLine(“Name: ” & person.Name)
Console.WriteLine(“Age: ” & person.Age)
Console.WriteLine(“City: ” & person.City)