前往官網下載dll
http://json.codeplex.com/
下載完成後會得到.zip,解壓縮後Bin\NetXX(XX=專案版本)資料夾底下的Newtonsoft.Json.dll放到Web Site專案的Bin目錄下完成加入參考。
Step.2 引用Json.NET相關的命名空間
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Step.3 建立Data Object
若資料格式為Array或DataTable,則跳過此步驟。
範例:
Public Class JsonDO
Private A As Integer = 0
Private B As String = ""
Private C As Integer = 0
Private item As Object
Public Property pA() As Integer
Get
Return A
End Get
Set(ByVal value As Integer)
A= value
End Set
End Property
Public Property pB() As String
Get
Return B
End Get
Set(ByVal value As String)
B= value
End Set
End Property
Public Property pC() As Integer
Get
Return C
End Get
Set(ByVal value As Integer)
C= value
End Set
End Property
Public Property pItem() As Object
Get
Return item
End Get
Set(ByVal value As Object)
item = value
End Set
End Property
End Class
Step.4 序列化Json字串
Dim str_json As String = JsonConvert.SerializeObject(陣列/資料表/物件 [, Formatting.Indented])
'Formatting.Indented,這樣的字串輸出在瀏覽器上檢視的話有排版效果較易閱讀
Step.5 傳送 & 接收
~~在此暫不紀錄~~
Step.6 解析(反序列化)Json字串
'轉為Array
Dim GetDataArray As JArray = JsonConvert.DeserializeObject(Of JArray)(Json_String)
'轉為DataTable
Dim GetDataTable As JArray = JsonConvert.DeserializeObject(Of DataTable)(Json_String)
'轉為Object
Dim GetObject As 物件名稱= JsonConvert.DeserializeObject(Of 物件名稱)(Json_String)
Step.7 讀取
Array/Object
'一般
變數 = GetDataArray .Item("key_value")
'如果要讀取第二層Item欄位中的細項可以這樣寫,那個 i 要給迴圈用的
變數 = GetDataArray .Item("key_value").ElementAt( i ).Item("key_value")
'若沒有key_value則(限Array使用)
變數 = GetDataArray( i ).Item("data_status")
DataTable
GridView_name.DataSource = GetDataTable
GridView_name.DataBind()
參考來源:
[VB.NET] JSON的產生與讀取匯入
沒有留言:
張貼留言