ASP.Net使它可以使用不同格式的jsonstring

时间:2013-01-15 12:51:54

标签: vb.net json

我创建了这个vb.net文件,当我发送这样的字符串时它正在工作:

?data={"id":"12345","timestamp":"2012-03-03 12:00:00","latitude":"23.41223","longitude"="54.12345"}

但我也想让它使用这样的格式:

?id=12345&timestamp=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

1 个答案:

答案 0 :(得分:1)

检查查询字符串。如果字符串以数据开头,则使用第一种方法将值分配给变量,否则通过获取查询字符串的值将值分配给变量。

示例:

If Not String.IsNullOrEmpty(Request.QueryString("data")) Then
    'Use reflection logic
Else
    MyObj.Longitude = Request.QueryString("longitude")
End If

修改

提供的Page_Load子例程中的实现示例。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    Dim myObj As New MyObject

    If Not String.IsNullOrEmpty(Request.QueryString("data")) Then
        Dim data As String = Request.QueryString("data")

        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
    Else
        If Not (String.IsNullOrEmpty(Request.QueryString("id")) Then
            myObj.ID = Request.QueryString("id")
        Else
            myObj.ID = "Not set"
        End If

        If Not (String.IsNullOrEmpty(Request.QueryString("longitude")) Then
            myObj.Longitude = Request.QueryString("longitude")
        Else
            myObj.Longitude = "Not set"
        End If

        If Not (String.IsNullOrEmpty(Request.QueryString("latitude")) Then
            myObj.Latitude = Request.QueryString("latitude")
        Else
            myObj.Latitude = "Not set"
        End If

        If Not (String.IsNullOrEmpty(Request.QueryString("timestamp")) Then
            myObj.Timestamp = Request.QueryString("timestamp")
        Else
            myObj.Timestamp = "Not set"
        End If  
    End If

    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)

    'Rest of sub here
End Sub