I still have constraints in making json data. What I have created is not recognized by POSTMAN or other json validator web. Please help what is wrong with my script.
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="pushRegistrasi.aspx.vb" Inherits="nsc.finance.pushRegistrasi" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>NSC Finance | Nusantara Sakti Group</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Script.Serialization
Imports System.Web.Configuration
Public Class pushRegistrasi
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dt As New DataTable()
dt = GetDataTable()
Dim jsonString As String = ConvertDataTableToJSON(dt)
'to display json data in taxtbox control
Context.Response.Write(jsonString)
End Sub
Public Function GetDataTable() As DataTable
Dim dataTable As New DataTable()
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("conn").ConnectionString)
Dim cmd As SqlCommand = conn.CreateCommand()
cmd.CommandText = "up_apl_push_data_registrasi"
cmd.CommandType = CommandType.StoredProcedure
If conn.State <> ConnectionState.Open Then
conn.Open()
End If
Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
dataTable.Load(dr)
End Using
Return dataTable
End Function
Public Function ConvertDataTableToJSON(dt As DataTable) As String
Dim jSonString As New JavaScriptSerializer()
Dim rows As New List(Of Dictionary(Of String, Object))()
Dim row As Dictionary(Of String, Object)
For Each dr As DataRow In dt.Rows
row = New Dictionary(Of String, Object)()
For Each col As DataColumn In dt.Columns
row.Add(col.ColumnName, dr(col))
Next
rows.Add(row)
Next
Return jSonString.Serialize(rows)
End Function
End Class
This is the Json URL address I created :
https://nscfinance.com/jsonFile/pushRegistrasi.aspx
The problem is when I want to show the data json to gridview it will display message "Additional text encountered after finished reading JSON content: <. Path '', line 3, position 0." And when I try to open using https://json-csv.com/ the message is the same.
My URL : https://nscfinance.com/jsonFile/pushRegistrasi.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>View Json Registrasi</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grdView" runat="server" AutoGenerateColumns="true" Width="100%" CellPadding="4" ForeColor="#333333" GridLines="None" EmptyDataText="Data Not Found">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
</div>
</form>
</body>
</html>
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
Public Class viewRegistrasi
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 dt As New DataTable()
dt.Columns.AddRange(New DataColumn(8) {New DataColumn("id_user", GetType(Integer)), New DataColumn("nama_user", GetType(String)),
New DataColumn("password", GetType(String)), New DataColumn("nama_depan", GetType(String)), New DataColumn("nama_belakang", GetType(String)),
New DataColumn("no_ktp", GetType(String)), New DataColumn("kode_kelamin", GetType(String)), New DataColumn("telepon", GetType(String)),
New DataColumn("email", GetType(String))})
Dim url As String = New System.Net.WebClient().DownloadString("https://nscfinance.com/jsonFile/pushRegistrasi.aspx")
Dim jsonArray As JArray = JArray.Parse(url)
For i As Integer = 0 To jsonArray.Count - 1
Dim isiData As Infos = (New JavaScriptSerializer()).Deserialize(Of Infos)(jsonArray(i).ToString())
Dim dr As DataRow = dt.NewRow()
dr("id_user") = isiData.id_user
dr("nama_user") = isiData.nama_user
dr("password") = isiData.password
dr("nama_depan") = isiData.nama_depan
dr("nama_belakang") = isiData.nama_belakang
dr("no_ktp") = isiData.no_ktp
dr("kode_kelamin") = isiData.kode_kelamin
dr("telepon") = isiData.telepon
dr("email") = isiData.email
dt.Rows.Add(dr)
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 id_user() As String
Get
Return m_id_user
End Get
Set(value As String)
m_id_user = value
End Set
End Property
Private m_id_user As String
Public Property nama_user() As String
Get
Return m_nama_user
End Get
Set(value As String)
m_nama_user = value
End Set
End Property
Private m_nama_user As String
Public Property password() As String
Get
Return m_password
End Get
Set(value As String)
m_password = value
End Set
End Property
Private m_password As String
Public Property nama_depan() As String
Get
Return m_nama_depan
End Get
Set(value As String)
m_nama_depan = value
End Set
End Property
Private m_nama_depan As String
Public Property nama_belakang() As String
Get
Return m_nama_belakang
End Get
Set(value As String)
m_nama_belakang = value
End Set
End Property
Private m_nama_belakang As String
Public Property no_ktp() As String
Get
Return m_no_ktp
End Get
Set(value As String)
m_no_ktp = value
End Set
End Property
Private m_no_ktp As String
Public Property kode_kelamin() As String
Get
Return m_kode_kelamin
End Get
Set(value As String)
m_kode_kelamin = value
End Set
End Property
Private m_kode_kelamin As String
Public Property telepon() As String
Get
Return m_telepon
End Get
Set(value As String)
m_telepon = value
End Set
End Property
Private m_telepon As String
Public Property email() As String
Get
Return m_email
End Get
Set(value As String)
m_email = value
End Set
End Property
Private m_email As String
End Class
End Class