我已经创建了这个 vb.net 文件,当我发送这样的字符串时它正在工作:
?data={"id":"12345","timestamp":"2012-03-03 12:00:00","latitude":"23.41223","longitude"="54.12345"}
但我也希望它使用这样的格式:
?id=12345×tamp=2012-03-03 12:00:00&latitude=23.41223&longitude=54.12345
我如何在我的 vb.net 文件中进行这项工作
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not String.IsNullOrEmpty(Request.QueryString("data")) Then
Dim data As String = Request.QueryString("data")
Dim myObj As New MyObject
Dim properties() As PropertyInfo = myObj.GetType().GetProperties()
Dim values() As String = Server.UrlDecode(data).Replace("[", "").Replace("]", "").Replace("{", "").Replace("}", "").Replace(""":""", """=""").Split(New Char() {","}, StringSplitOptions.RemoveEmptyEntries)
For Each value As String In values
Dim keyValue() As String = value.Split(New Char() {"="}, StringSplitOptions.RemoveEmptyEntries)
For Each prop As PropertyInfo In properties
If prop.Name.ToLower = keyValue(0).ToLower.Replace("""", "") Then
prop.SetValue(myObj, keyValue(1).Replace("""", ""), Nothing)
End If
Next
Next
lblText1.Text = String.Format("ID: {0}", myObj.ID)
lblText2.Text = String.Format("Longitude: {0}", myObj.Longitude)
lblText3.Text = String.Format("Latitude: {0}", myObj.Latitude)
lblText4.Text = String.Format("Timestamp: {0}", myObj.Timestamp)
Cmd.Parameters.Clear()
Cmd.Parameters.AddWithValue("@ID", myObj.ID)
Cmd.Parameters.AddWithValue("@Longitude", myObj.Longitude)
Cmd.Parameters.AddWithValue("@Latitude", myObj.Latitude)
Cmd.Parameters.AddWithValue("@Timestamp", myObj.Timestamp)
Con.ConnectionString = "Data Source=servert\sql;Initial Catalog=table;Integrated Security=True"
Cmd.Connection = Con
Con.Open()
Cmd.CommandText = "IF EXISTS (SELECT 1 FROM Locatie WHERE id = @ID) " & Environment.NewLine & _
" BEGIN UPDATE Locatie SET Longitude = @Longitude, Latitude = @Latitude, Timestamp = @Timestamp WHERE id=@ID END " & Environment.NewLine & _
"ELSE " & Environment.NewLine & _
" BEGIN INSERT INTO Locatie VALUES (@ID, @Longitude, @Latitude, @Timestamp) END "
Reader = Cmd.ExecuteReader
Reader.Close()
Con.Close()
Con.Dispose()
End If
End Sub
Public Class MyObject
Private _ID As String
Private _Longitude As String
Private _Latitude As String
Private _Timestamp As String
Public Property ID As String
Get
Return _ID
End Get
Set(value As String)
_ID = value
End Set
End Property
Public Property Longitude As String
Get
Return _Longitude
End Get
Set(value As String)
_Longitude = value
End Set
End Property
Public Property Latitude As String
Get
Return _Latitude
End Get
Set(value As String)
_Latitude = value
End Set
End Property
Public Property Timestamp As String
Get
Return _Timestamp
End Get
Set(value As String)
_Timestamp = value
End Set
End Property
End Class
</script>
可能键必须在此处,因此还有第二个选项,其中第二个字符串被拆分,将值设置为 id、经度、纬度和时间戳:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not String.IsNullOrEmpty(Request.QueryString("data")) Then
Dim data As String = Request.QueryString("data")
Dim myObj As New MyObject
Dim properties() As PropertyInfo = myObj.GetType().GetProperties()
Dim values() As String = Server.UrlDecode(data).Replace("[", "").Replace("]", "").Replace("{", "").Replace("}", "").Replace(""":""", """=""").Split(New Char() {","}, StringSplitOptions.RemoveEmptyEntries)
For Each value As String In values
Dim keyValue() As String = value.Split(New Char() {"="}, StringSplitOptions.RemoveEmptyEntries)
For Each prop As PropertyInfo In properties
If prop.Name.ToLower = keyValue(0).ToLower.Replace("""", "") Then
prop.SetValue(myObj, keyValue(1).Replace("""", ""), Nothing)
End If
Next
Next