------------------ aspx請求画面 ------------------
<asp:Content ID=・・・略>
<script type="text/jscript">
$(function () {
$("#btnSearch").click(function () {
$.ajax({
url: "AjaxLogicUtil.aspx",
type: "GET",
contentType: "application/json; charset=utf-8",
success: function (data) {
if (data != null) {
for (var j = 0; j < data.length; j++) {
alert(data[j].col1 + "/" + data[j].col2 + "/" + data[j].col3 + "/" + data[j].col4 + "/" + data[j].col5);
}
} else {
alert('There is not json data now!');
}
},
error: function () { alert('get json data error!'); }
});
});
});//end
<script>
</asp:Content>
------------------ json処理Aspx画面类 ------------------
'說明:Json処理画面新規が必要がある(AjaxLogicUtil.aspx)
Public Class AjaxLogicUtil
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim parType As String = Request.QueryString("JsonParType").ToString()
Dim parValue As String = Request.QueryString("JsonParValue").ToString()
Dim dt As New DataTable()
dt = NewTB()
'Datatable data をJson種類にシリアライザ(Serializer)
Dim jsonStr As String = SerializerDatatableToJson(dt)
Response.Clear()
Response.Write(jsonStr)
Response.End()
End Sub
’テストデータテーブル
Public Shared Function NewTB() As DataTable
Dim tb As New DataTable
tb.Columns.Add("col1")
tb.Columns.Add("col2")
tb.Columns.Add("col3")
tb.Columns.Add("col4")
tb.Columns.Add("col5")
tb.Rows.Add("1", "aaa", "bigg", "ggggss", "sssss")
tb.Rows.Add("2", "bbb", "smll", "llllll", "44444")
tb.Rows.Add("3", "ccc", "dogss", "sssss", "k6666")
tb.Rows.Add("4", "eee", "yyyr", "343as", "y78ghj")
Return tb
End Function
'把datatable 数据序列化为json(VB)
Public Shadows Function SerializerDatatableToJson(ByVal dt As DataTable) As String
Dim sJson As New StringBuilder()
If dt.Rows.Count > 0 Then
sJson.Append("[")
For i = 0 To dt.Rows.Count - 1
sJson.Append("{")
For j = 0 To dt.Columns.Count - 1
sJson.Append("""")
sJson.Append(dt.Columns(j).ColumnName)
sJson.Append("""")
sJson.Append(":")
sJson.Append("""")
sJson.Append(dt.Rows(i)(j).ToString())
sJson.Append("""")
If j <> dt.Columns.Count - 1 Then
sJson.Append(",")
End If
Next
sJson.Append("}")
sJson.Append(",")
Next
sJson.Remove(sJson.Length - 1, 1)
sJson.Append("]")
Return sJson.ToString()
Else
Return "[]"
End If
End Function
End Class
---------------------------------
//把datatable 数据序列化为json(C#)
public static string Datatabletojson(DataTable dt)
{
StringBuilder sJson = new StringBuilder();
if (dt.Rows.Count > 0) {
sJson.Append("[");
for (int i = 0; i < dt.Rows.Count; i++)
{
sJson.Append('{');
for (int j = 0; j < dt.Columns.Count; j++)
{
sJson.Append('"');
sJson.Append(dt.Columns[j].ColumnName);
sJson.Append('"');
sJson.Append(':');
sJson.Append('"');
sJson.Append(dt.Rows[i][j].ToString());
sJson.Append('"');
if (j != dt.Columns.Count - 1)
{
sJson.Append(',');
}
}
sJson.Append('}');
sJson.Append(',');
}
}
sJson.Remove(sJson.Length - 1, 1);
sJson.Append(']');
return sJson.ToString();
}
<asp:Content ID=・・・略>
<script type="text/jscript">
$(function () {
$("#btnSearch").click(function () {
$.ajax({
url: "AjaxLogicUtil.aspx",
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "json",
async:false,//同步调用
data: { JsonParType: 'a_type', JsonParValue: 111 },success: function (data) {
if (data != null) {
for (var j = 0; j < data.length; j++) {
alert(data[j].col1 + "/" + data[j].col2 + "/" + data[j].col3 + "/" + data[j].col4 + "/" + data[j].col5);
}
} else {
alert('There is not json data now!');
}
},
error: function () { alert('get json data error!'); }
});
});
});//end
<script>
</asp:Content>
------------------ json処理Aspx画面类 ------------------
'說明:Json処理画面新規が必要がある(AjaxLogicUtil.aspx)
Public Class AjaxLogicUtil
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim parType As String = Request.QueryString("JsonParType").ToString()
Dim parValue As String = Request.QueryString("JsonParValue").ToString()
Dim dt As New DataTable()
dt = NewTB()
'Datatable data をJson種類にシリアライザ(Serializer)
Dim jsonStr As String = SerializerDatatableToJson(dt)
Response.Clear()
Response.Write(jsonStr)
Response.End()
End Sub
’テストデータテーブル
Public Shared Function NewTB() As DataTable
Dim tb As New DataTable
tb.Columns.Add("col1")
tb.Columns.Add("col2")
tb.Columns.Add("col3")
tb.Columns.Add("col4")
tb.Columns.Add("col5")
tb.Rows.Add("1", "aaa", "bigg", "ggggss", "sssss")
tb.Rows.Add("2", "bbb", "smll", "llllll", "44444")
tb.Rows.Add("3", "ccc", "dogss", "sssss", "k6666")
tb.Rows.Add("4", "eee", "yyyr", "343as", "y78ghj")
Return tb
End Function
'把datatable 数据序列化为json(VB)
Public Shadows Function SerializerDatatableToJson(ByVal dt As DataTable) As String
Dim sJson As New StringBuilder()
If dt.Rows.Count > 0 Then
sJson.Append("[")
For i = 0 To dt.Rows.Count - 1
sJson.Append("{")
For j = 0 To dt.Columns.Count - 1
sJson.Append("""")
sJson.Append(dt.Columns(j).ColumnName)
sJson.Append("""")
sJson.Append(":")
sJson.Append("""")
sJson.Append(dt.Rows(i)(j).ToString())
sJson.Append("""")
If j <> dt.Columns.Count - 1 Then
sJson.Append(",")
End If
Next
sJson.Append("}")
sJson.Append(",")
Next
sJson.Remove(sJson.Length - 1, 1)
sJson.Append("]")
Return sJson.ToString()
Else
Return "[]"
End If
End Function
End Class
---------------------------------
//把datatable 数据序列化为json(C#)
public static string Datatabletojson(DataTable dt)
{
StringBuilder sJson = new StringBuilder();
if (dt.Rows.Count > 0) {
sJson.Append("[");
for (int i = 0; i < dt.Rows.Count; i++)
{
sJson.Append('{');
for (int j = 0; j < dt.Columns.Count; j++)
{
sJson.Append('"');
sJson.Append(dt.Columns[j].ColumnName);
sJson.Append('"');
sJson.Append(':');
sJson.Append('"');
sJson.Append(dt.Rows[i][j].ToString());
sJson.Append('"');
if (j != dt.Columns.Count - 1)
{
sJson.Append(',');
}
}
sJson.Append('}');
sJson.Append(',');
}
}
sJson.Remove(sJson.Length - 1, 1);
sJson.Append(']');
return sJson.ToString();
}