JSON data not mapping to Class Property in VB.Net

Last Reply 7 months ago By kalpesh

Posted 7 months ago

Why json data that appears only numeric while the string type and date not appear. URL : http://222.124.22.166/nsapi?type=INFOPK&noktp=3202110611900004

and the result is:

{
"error":"false",
"error_message":"",
"MASTER":
[{"NoKTP":"3202110611900004","Nama":"SUGIRI","AlamatMaster":"KP SEKARWANGI"}],
"DETAIL":
[{"NoKTP":"3202110611900004","NamaKonsumen":"SUGIRI","NoFaktur":"2216080000241","Jenis":"","TanggalBeli":"2016-08-09","TanggalJTP":"0000-00-00","TipeMotor":"REVO","Angsuran":"573000","Tenor":"11","Ke":"0","StatusFaktur":"BTL","Plat":""},
{"NoKTP":"3202110611900004","NamaKonsumen":"SUGIRI","NoFaktur":"2215080000315","Jenis":"BEKAS","TanggalBeli":"2015-08-10","TanggalJTP":"2015-09-10","TipeMotor":"ABS REVO","Angsuran":"606000","Tenor":"12","Ke":"12","StatusFaktur":"CLSD","Plat":"F6631VZ"}]
}

My Script :

Imports System.Web.Services
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web
Imports System.Web.Script.Serialization
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Net
Imports Newtonsoft.Json.Linq
Imports System.Globalization
 
Public Class jsonKsystem
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindData()
        End If
    End Sub
 
    Private Sub BindData()
        Dim isiKTP As String = "3202110611900004"
        Dim dt As New DataTable()
 
        dt.Columns.AddRange(New DataColumn(12) {New DataColumn("error", GetType(String)), New DataColumn("error_message", GetType(String)),
            New DataColumn("MASTER_NoKTP", GetType(String)), New DataColumn("MASTER_Nama", GetType(String)),
            New DataColumn("MASTER_AlamatMaster", GetType(String)), New DataColumn("DETAIL_TanggalBeli", GetType(Date)),
            New DataColumn("DETAIL_TanggalJTP", GetType(Date)), New DataColumn("DETAIL_TipeMotor", GetType(String)),
            New DataColumn("DETAIL_Angsuran", GetType(Double)), New DataColumn("DETAIL_Tenor", GetType(Double)),
            New DataColumn("DETAIL_StatusFaktur", GetType(String)),
            New DataColumn("DETAIL_Ke", GetType(Double)), New DataColumn("DETAIL_Plat", GetType(String))})
 
        Dim url As String = New System.Net.WebClient().DownloadString("http://222.124.22.166/nsapi?type=INFOPK&noktp=" & isiKTP)
        Dim jsonArray As JArray = JArray.Parse("[" + url.Trim() + "]")
 
        For i As Integer = 0 To jsonArray.Count - 1
            Dim isiData As Infos = (New JavaScriptSerializer()).Deserialize(Of Infos)(jsonArray(i).ToString())
            For j As Integer = 0 To isiData.detail.Count - 1
                Dim dr As DataRow = dt.NewRow()
                dr("error") = isiData.error
                dr("error_message") = isiData.error_message
                dr("MASTER_NoKTP") = isiData.master_ktp
                dr("MASTER_Nama") = isiData.master_nama
                dr("MASTER_AlamatMaster") = isiData.master_alamat
                dr("DETAIL_TanggalBeli") = Convert.ToDateTime(isiData.detail(j).tgl_beli, CultureInfo.CurrentCulture).ToString("yyyy-MM-dd")
                dr("DETAIL_TanggalJTP") = Convert.ToDateTime(isiData.detail(j).tgl_jtp, CultureInfo.CurrentCulture).ToString("yyyy-MM-dd")
                dr("DETAIL_TipeMotor") = isiData.detail(j).tipe_motor
                dr("DETAIL_Angsuran") = isiData.detail(j).angsuran
                dr("DETAIL_Tenor") = isiData.detail(j).tenor
                dr("DETAIL_Ke") = isiData.detail(j).ke
                dr("DETAIL_StatusFaktur") = isiData.detail(j).sts_faktur
                dr("DETAIL_Plat") = isiData.detail(j).plat_no
                dt.Rows.Add(dr)
            Next
        Next
 
        If dt.Rows.Count > 0 Then
            'Bind DataTable to your GridView
            grdView.DataSource = dt
            grdView.DataBind()
        End If
    End Sub
 
    Public Class Infos
        Public Property error_message() As String
            Get
                Return m_error_message
            End Get
            Set(value As String)
                m_error_message = value
            End Set
        End Property
        Private m_error_message As String
 
        Public Property [error]() As String
            Get
                Return m_error
            End Get
            Set(value As String)
                m_error = value
            End Set
        End Property
        Private m_error As String
 
        Public Property master_ktp() As String
            Get
                Return m_master_ktp
            End Get
            Set(value As String)
                m_master_ktp = value
            End Set
        End Property
        Private m_master_ktp As String
 
        Public Property master_nama() As String
            Get
                Return m_master_nama
            End Get
            Set(value As String)
                m_master_nama = value
            End Set
        End Property
        Private m_master_nama As String
 
        Public Property master_alamat() As String
            Get
                Return m_master_alamat
            End Get
            Set(value As String)
                m_master_alamat = value
            End Set
        End Property
        Private m_master_alamat As String
 
        Private m_detail As List(Of Detail)
        Public Property detail() As List(Of Detail)
            Get
                Return m_detail
            End Get
            Set(value As List(Of Detail))
                m_detail = value
            End Set
        End Property
 
    End Class
 
    Public Class Detail
        Public Property tgl_beli() As String
            Get
                Return m_tgl_beli
            End Get
            Set(value As String)
                m_tgl_beli = value
            End Set
        End Property
        Private m_tgl_beli As String
 
        Public Property tgl_jtp() As String
            Get
                Return m_tgl_jtp
            End Get
            Set(value As String)
                m_tgl_jtp = value
            End Set
        End Property
        Private m_tgl_jtp As String
 
        Public Property tipe_motor() As String
            Get
                Return m_tipe_motor
            End Get
            Set(value As String)
                m_tipe_motor = value
            End Set
        End Property
        Private m_tipe_motor As String
 
        Public Property angsuran() As String
            Get
                Return m_angsuran
            End Get
            Set(value As String)
                m_angsuran = value
            End Set
        End Property
        Private m_angsuran As String
 
        Public Property tenor() As String
            Get
                Return m_tenor
            End Get
            Set(value As String)
                m_tenor = value
            End Set
        End Property
        Private m_tenor As String
 
        Public Property ke() As String
            Get
                Return m_ke
            End Get
            Set(value As String)
                m_ke = value
            End Set
        End Property
        Private m_ke As String
 
        Public Property sts_faktur() As String
            Get
                Return m_sts_faktur
            End Get
            Set(value As String)
                m_sts_faktur = value
            End Set
        End Property
        Private m_sts_faktur As String
 
        Public Property plat_no() As String
            Get
                Return m_plat_no
            End Get
            Set(value As String)
                m_plat_no = value
            End Set
        End Property
        Private m_plat_no As String
    End Class
End Class

The Result : error     error_message     MASTER_NoKTP     MASTER_Nama     MASTER_AlamatMaster     DETAIL_TanggalBeli     DETAIL_TanggalJTP     DETAIL_TipeMotor     DETAIL_Angsuran     DETAIL_Tenor     DETAIL_StatusFaktur     DETAIL_Ke     DETAIL_Plat FALSE                             01/01/0001 00:00:00     01/01/0001 00:00:00           573000     11           0      FALSE                             01/01/0001 00:00:00     01/01/0001 00:00:00           606000     12           12

You are viewing reply posted by: kalpesh 7 months ago.
Posted 7 months ago Modified on 7 months ago

Hi arie.keren,

Problem is in your Property name which you are assigning in Details class also you need to create master Class object too with property name as same as it comes in json object.

Eg. tgl_beli() need to be tanggalBeli as in json result it’s come like TanggalBeli also tgl_jtp() need to be tanggalJTP as in json result it’s come like TanggalJT And same for rest propery too.

Refer the below sample example for your reference.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        BindData()
    End If
End Sub

Private Sub BindData()
    Dim isiKTP As String = "3202110611900004"
    Dim dt As New DataTable()

    dt.Columns.AddRange(New DataColumn(12) {New DataColumn("error", GetType(String)), New DataColumn("error_message", GetType(String)),
        New DataColumn("MASTER_NoKTP", GetType(String)), New DataColumn("MASTER_Nama", GetType(String)),
        New DataColumn("MASTER_AlamatMaster", GetType(String)), New DataColumn("DETAIL_TanggalBeli", GetType(Date)),
        New DataColumn("DETAIL_TanggalJTP", GetType(Date)), New DataColumn("DETAIL_TipeMotor", GetType(String)),
        New DataColumn("DETAIL_Angsuran", GetType(Double)), New DataColumn("DETAIL_Tenor", GetType(Double)),
        New DataColumn("DETAIL_StatusFaktur", GetType(String)),
        New DataColumn("DETAIL_Ke", GetType(Double)), New DataColumn("DETAIL_Plat", GetType(String))})
    'currently provided link not working so i read json value from txt file which you have provided.
    Dim url As String = New System.Net.WebClient().DownloadString("http://222.124.22.166/nsapi?type=INFOPK&noktp=" & isiKTP)
    'Copy the result of json string if url not exist for json result and read by using System.IO.File.ReadAllText for testing purpose
    'Dim url As String = System.IO.File.ReadAllText(Server.MapPath("~/Json855546.txt"))

    Dim jsonArray As JArray = JArray.Parse("[" + url.Trim() + "]")

    For i As Integer = 0 To jsonArray.Count - 1
        Dim isiData As Infos = (New JavaScriptSerializer()).Deserialize(Of Infos)(jsonArray(i).ToString())
        For j As Integer = 0 To isiData.detail.Count - 1
            Dim dr As DataRow = dt.NewRow()
            Dim dateValue As Date
            dr("error") = isiData.error
            dr("error_message") = isiData.error_message
            dr("MASTER_NoKTP") = isiData.Master(i).NoKTP
            dr("MASTER_Nama") = isiData.Master(i).Nama
            dr("MASTER_AlamatMaster") = isiData.Master(i).AlamatMaster

            If Date.TryParse(isiData.detail(j).tanggalBeli, dateValue) Then
                dr("DETAIL_TanggalBeli") = Convert.ToDateTime(isiData.detail(j).tanggalBeli, CultureInfo.CurrentCulture).ToString("yyyy-MM-dd")
            Else
                dr("DETAIL_TanggalBeli") = DBNull.Value
            End If

            If Date.TryParse(isiData.detail(j).tanggalJTP, dateValue) Then
                dr("DETAIL_TanggalJTP") = Convert.ToDateTime(isiData.detail(j).tanggalJTP, CultureInfo.CurrentCulture).ToString("yyyy-MM-dd")
            Else
                dr("DETAIL_TanggalJTP") = DBNull.Value
            End If
            dr("DETAIL_TipeMotor") = isiData.detail(j).tipeMotor
            dr("DETAIL_Angsuran") = isiData.detail(j).angsuran
            dr("DETAIL_Tenor") = isiData.detail(j).tenor
            dr("DETAIL_Ke") = isiData.detail(j).ke
            dr("DETAIL_StatusFaktur") = isiData.detail(j).statusFaktur
            dr("DETAIL_Plat") = isiData.detail(j).plat
            dt.Rows.Add(dr)
        Next
    Next

    If dt.Rows.Count > 0 Then
        'Bind DataTable to your GridView
        grdView.DataSource = dt
        grdView.DataBind()
    End If
End Sub

Public Class Infos
    Public Property error_message() As String
        Get
            Return m_error_message
        End Get
        Set(value As String)
            m_error_message = value
        End Set
    End Property
    Private m_error_message As String

    Public Property [error]() As String
        Get
            Return m_error
        End Get
        Set(value As String)
            m_error = value
        End Set
    End Property
    Private m_error As String

    Private m_detail As List(Of Detail)
    Public Property detail() As List(Of Detail)
        Get
            Return m_detail
        End Get
        Set(value As List(Of Detail))
            m_detail = value
        End Set
    End Property

    Private m_master As List(Of Master1)
    Public Property Master() As List(Of Master1)
        Get
            Return m_master
        End Get
        Set(value As List(Of Master1))
            m_master = value
        End Set
    End Property
End Class

Public Class Detail
    Public Property tanggalBeli() As String
        Get
            Return m_tgl_beli
        End Get
        Set(value As String)
            m_tgl_beli = value
        End Set
    End Property
    Private m_tgl_beli As String

    Public Property tanggalJTP() As String
        Get
            Return m_tgl_jtp
        End Get
        Set(value As String)
            m_tgl_jtp = value
        End Set
    End Property
    Private m_tgl_jtp As String

    Public Property tipeMotor() As String
        Get
            Return m_tipe_motor
        End Get
        Set(value As String)
            m_tipe_motor = value
        End Set
    End Property
    Private m_tipe_motor As String

    Public Property angsuran() As String
        Get
            Return m_angsuran
        End Get
        Set(value As String)
            m_angsuran = value
        End Set
    End Property
    Private m_angsuran As String

    Public Property tenor() As String
        Get
            Return m_tenor
        End Get
        Set(value As String)
            m_tenor = value
        End Set
    End Property
    Private m_tenor As String

    Public Property ke() As String
        Get
            Return m_ke
        End Get
        Set(value As String)
            m_ke = value
        End Set
    End Property
    Private m_ke As String

    Public Property statusFaktur() As String
        Get
            Return m_sts_faktur
        End Get
        Set(value As String)
            m_sts_faktur = value
        End Set
    End Property
    Private m_sts_faktur As String

    Public Property plat() As String
        Get
            Return m_plat_no
        End Get
        Set(value As String)
            m_plat_no = value
        End Set
    End Property
    Private m_plat_no As String
End Class
Private m_NoKTP As String

Public Class Master1
    Public Property NoKTP() As String
        Get
            Return m_master_ktp
        End Get
        Set(value As String)
            m_master_ktp = value
        End Set
    End Property
    Private m_master_ktp As String

    Public Property Nama() As String
        Get
            Return m_master_nama
        End Get
        Set(value As String)
            m_master_nama = value
        End Set
    End Property
    Private m_master_nama As String

    Public Property AlamatMaster() As String
        Get
            Return m_master_alamat
        End Get
        Set(value As String)
            m_master_alamat = value
        End Set
    End Property
    Private m_master_alamat As String
End Class

Output

error error_message MASTER_NoKTP MASTER_Nama MASTER_AlamatMaster DETAIL_TanggalBeli DETAIL_TanggalJTP DETAIL_TipeMotor DETAIL_Angsuran DETAIL_Tenor DETAIL_StatusFaktur DETAIL_Ke DETAIL_Plat
FALSE   3202110611900004 SUGIRI KP SEKARWANGI 8/9/2016 12:00:00 AM   REVO 573000 11 BTL 0  
FALSE   3202110611900004 SUGIRI KP SEKARWANGI 8/10/2015 12:00:00 AM 9/10/2015 12:00:00 AM ABS REVO 606000 12 CLSD 12 F6631VZ