在.net中有提供一個JSON的物件可以做JSON的serialize跟Deserialize,
要做deserilize可以把json 的字串轉.net中的物件
一般一維的json物件可以直接轉成 dictionary(of string,string)來存放
如下面的code
陣列物件可以轉成list(of dictionary(of string,string))來存放
比較複雜的物件,則可能需要自行建立一個class來作為存放的依據
要做deserilize可以把json 的字串轉.net中的物件
一般一維的json物件可以直接轉成 dictionary(of string,string)來存放
如下面的code
Dim jsonString As String = " {'Name': 'Jason','Title':'Mr,','Age':'25'}"
Dim JSONSerializer As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim JSON_obj = JSONSerializer.Deserialize(Of Dictionary(Of String, String))(jsonString)
陣列物件可以轉成list(of dictionary(of string,string))來存放
Dim jsonString As String = " [{'Name': 'Jason','Title':'Mr,','Age':'25'},{'Name': 'John','Title':'Mr,','Age':'55'}]"
Dim JSONSerializer As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim JSON_obj = JSONSerializer.Deserialize(Of Dictionary(Of String, String))(jsonString)
比較複雜的物件,則可能需要自行建立一個class來作為存放的依據
Public Class jsonobj
Property Name() As String
Property Title() As String
Property Age() As String
Property Phone() As List(Of Dictionary(Of String, String))
End Class
Dim jsonString As String = " {'Name': 'Jason','Title':'Mr,','Age':'25','Phone':[{'title': 'home','value':'091236'},{'title': 'mobile','value':'12345777'}]}"
Dim JSONSerializer As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim JSON_obj = JSONSerializer.Deserialize(Of jsonobj)(jsonString)
留言