Do more with
Class JsonData
Public Property Status() As String
Public Property [Product]() As Product
End Class
Class Product
Public Property Name() As String
Public Property Code() As String
Public Property Price() As String
Public Property Active() As Boolean
Public Property [Contact]() As Contact
End Class
Class Contact
Public Property Name() As String
Public Property Phone() As String
Public Property Email() As String
End Class
Imports Newtonsoft.Json
Imports System.Reflection
Imports System.Runtime.CompilerServices
Module Module1
Const json = "{""status"": ""OK"",""product"":{""name"": ""ABC"",""code"": ""A"",""Price"": ""2.5"",""active"": true,""contact"":{""name"": ""X"",""phone"": ""999-999-9999"",""email"": ""X@company.com""}}}"
Sub Main()
Dim response = JsonConvert.DeserializeObject(Of JsonData)(json)
Console.WriteLine(response.ToString())
Console.ReadLine()
End Sub
End Module
Class JsonData
Public Property Status() As String
Public Property [Product]() As Product
Public Overrides Function ToString() As String
Return Me.GetPropertiesString()
End Function
End Class
Class Product
Public Property Name() As String
Public Property Code() As String
Public Property Price() As String
Public Property Active() As Boolean
Public Property [Contact]() As Contact
Public Overrides Function ToString() As String
Return Me.GetPropertiesString()
End Function
End Class
Class Contact
Public Property Name() As String
Public Property Phone() As String
Public Property Email() As String
Public Overrides Function ToString() As String
Return Me.GetPropertiesString()
End Function
End Class
Module Extensions
<Extension()> _
Public Function GetPropertiesString(Of T)(source As T) As String
Dim result = String.Empty
If source IsNot Nothing Then
result = String.Join("; ", (From [property] As PropertyInfo In GetType(T).GetProperties() Select String.Format("{{ {0}, {1} }}", [property].Name, [property].GetValue(source, Nothing))))
End If
Return result
End Function
End Module
Produces the following output -public class Contact
{
public string name { get; set; }
public string phone { get; set; }
public string email { get; set; }
}
public class Product
{
public string name { get; set; }
public string code { get; set; }
public string Price { get; set; }
public bool active { get; set; }
public Contact contact { get; set; }
}
public class RootObject
{
public string status { get; set; }
public Product product { get; set; }
}
Imports Newtonsoft.Json
Imports System.Reflection
Imports System.Runtime.CompilerServices
Module Module1
Const json = "[{""status"": ""OK"",""product"":{""name"": ""ABC"",""code"": ""A"",""Price"": ""2.5"",""active"": true,""contact"":{""name"": ""X"",""phone"": ""999-999-9999"",""email"": ""X@company.com""}}},{""status"": ""FAIL"",""product"":{""name"": ""DEF"",""code"": ""D"",""Price"": ""3.5"",""active"": true,""contact"":{""name"": ""Y"",""phone"": ""999-999-9999"",""email"": ""Y@company.com""}}},{""status"": ""OK"",""product"":{""name"": ""GHI"",""code"": ""G"",""Price"": ""1.5"",""active"": false,""contact"":{""name"": ""Z"",""phone"": ""999-999-9999"",""email"": ""Z@company.com""}}},{""status"": ""FAIL"",""product"":{""name"": ""JKL"",""code"": ""J"",""Price"": ""4.5"",""active"": true,""contact"":{""name"": ""A"",""phone"": ""999-999-9999"",""email"": ""A@company.com""}}},{""status"": ""OK"",""product"":{""name"": ""MNO"",""code"": ""M"",""Price"": ""0.5"",""active"": false,""contact"":{""name"": ""B"",""phone"": ""999-999-9999"",""email"": ""B@company.com""}}}]"
Sub Main()
Dim response = JsonConvert.DeserializeObject(Of Products)(json)
Console.WriteLine("FAILURES")
For Each [product] In response.Where(Function(x) x.Status.Equals("fail", StringComparison.OrdinalIgnoreCase))
Console.WriteLine([product])
Next
Console.WriteLine()
Console.WriteLine("IN-ACTIVE")
For Each [product] In (From p In response Where Not p.Product.Active Select p)
Console.WriteLine([product])
Next
Console.WriteLine()
Console.WriteLine("COST LESS THAN 2")
For Each [product] In (From p In response Where Convert.ToDecimal(p.Product.Price) < 2D Select p)
Console.WriteLine([product])
Next
Console.ReadLine()
End Sub
End Module
Class Products
Inherits List(Of JsonData)
Public Sub New()
MyBase.New()
End Sub
Public Sub New(capacity As Integer)
MyBase.New(capacity)
End Sub
Public Sub New(collection As IEnumerable(Of JsonData))
MyBase.New(collection)
End Sub
End Class
Class JsonData
Public Property Status() As String
Public Property [Product]() As Product
Public Overrides Function ToString() As String
Return Me.GetPropertiesString()
End Function
End Class
Class Product
Public Property Name() As String
Public Property Code() As String
Public Property Price() As String
Public Property Active() As Boolean
Public Property [Contact]() As Contact
Public Overrides Function ToString() As String
Return Me.GetPropertiesString()
End Function
End Class
Class Contact
Public Property Name() As String
Public Property Phone() As String
Public Property Email() As String
Public Overrides Function ToString() As String
Return Me.GetPropertiesString()
End Function
End Class
Module Extensions
<Extension()> _
Public Function GetPropertiesString(Of T)(source As T) As String
Dim result = String.Empty
If source IsNot Nothing Then
result = String.Join("; ", (From [property] As PropertyInfo In GetType(T).GetProperties() Select String.Format("{{ {0}, {1} }}", [property].Name, [property].GetValue(source, Nothing))))
End If
Return result
End Function
End Module
Which produces the following output -
Premium Content
You need an Expert Office subscription to comment.Start Free Trial