Vb.net Net使用不同格式的jsonstring进行工作

Vb.net Net使用不同格式的jsonstring进行工作,vb.net,json,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 但我也希望它能以这样的格式工作: ?data={

我已经创建了这个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
但我也希望它能以这样的格式工作:

?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>  

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

例如:

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

我没有错误,但我没有得到任何回报。我怎样才能返回它,这样我就可以看到值是什么?可能是因为设置MyObj的功能在If中,如果它包含数据…?是的,您需要将其移出,并相应地重新整理代码。我已经编辑了我的代码,让您了解实现。我希望有帮助!好吧,我有个小问题。id=12345×tamp=2012-03-03 12:00:00&latitude=23.41223&latitude=54.12345的代码正在工作,唯一的问题是第一个字符串不再工作了,是不是因为在If Not string.IsNullOrEmpty(Request.QueryString(“data”))之后,它还会因为下一个字符串而触发else函数?