vb.net中的request.querystring

时间:2012-10-03 12:59:00

标签: vb.net json request.querystring

我有这个我需要解码的网址:

http://gistest:54321/default.aspx?data=%7B%22id%22:%2269403%22,%22longitude%22:%22-143.406417%22,%22latitude%22:%2232.785834%22,%22timestamp%22:%2223-10%2010:12%22%7D

此代码每次都会更改

我使用此代码:

<%Response.Write(Request.QueryString.Item("data") )%><br/>
<%Response.Write(Request.QueryString.Item("id") )%><br/>
<%Response.Write(Request.QueryString.Item("longitude") )%><br/>
<%Response.Write(Request.QueryString.Item("latitude") )%><br/>
<%Response.Write(Request.QueryString.Item("timestamp") )%><br/>

但我只把它作为输出,也许有一个选项可以检查数据是否为空,然后我request.querystring数据中的其他部分:

{"id"="69403","longitude"="-143.406417","latitude"="32.785834","timestamp"="23-10 10:12"}

这是来自

  

&lt;%Response.Write(Request.QueryString.Item(“data”))%&gt;

2 个答案:

答案 0 :(得分:1)

我认为您遇到的问题是您发布的网址中只有一个查询字符串参数,即data。其余信息以data查询字符串值编码。存储在data中的值几乎看起来像JSON / Javascript对象,但属性名称和值之间只有=而不是:

因此,基本上您无法使用Request.QueryString来获取idlongitudelatitude等的值。我认为您的选择是编写一些代码来自己解析data的值或用=替换:并使用.NET的JSON解析器(即JavascriptSerializer类或{{ 1}})。

就个人而言,我会在代码隐藏中编写一个返回JSON.net的方法。在该方法中,我只需将每个Dictionary(Of String, Object)更改为"=",然后使用.NET提供的":"来解析字符串。我现在没有方便的ASP.NET实例,但是我在LinqPad中汇总的以下示例应该说明这个想法:

JavaScriptSerializer

关于这种方法需要注意的一点是,我希望网址与您发布的网址格式相同。您可能需要修改此方法以使其更加健壮以处理不同的输入。

如果您将Sub Main Dim url = "http://gistest:54321/default.aspx?data=%7B%22id%22=%2269403%22,%22longitude%22=%22-143.406417%22,%22latitude%22=%2232.785834%22,%22timestamp%22=%2223-10%2010:12%22%7D" Dim uri = New Uri(url) Dim data = System.Web.HttpUtility.ParseQueryString(uri.Query)("data") Dim o = ParseData(data) Console.WriteLine(o("id")) Console.WriteLine(o("longitude")) Console.WriteLine(o("latitude")) Console.WriteLine(o("timestamp")) End Sub Function ParseData(data As String) As Dictionary(Of String, Object) Dim js = new System.Web.Script.Serialization.JavaScriptSerializer() Dim o = js.DeserializeObject(data.Replace("""=""", """:""")) ParseData = DirectCast(o, Dictionary(Of String, Object)) End Function 功能放入代码隐藏中,那么首页中的以下代码应该为您提供所需的输出(再次,抱歉,我不知道)现在要测试一个ASP.NET实例):

ParseData

答案 1 :(得分:1)

我真的希望我能正确理解这个问题。我假设你需要在名为Data的查询字符串键中的每个键的值?为此,我使用了以下代码:

    Dim values() As String = Server.UrlDecode(Request.QueryString("data")).Replace("{", "").Replace("}", "").Split(New Char() {","}, StringSplitOptions.RemoveEmptyEntries)
    For Each value As String In values
        Dim keyValue() As String = value.Split(New Char() {":"}, StringSplitOptions.RemoveEmptyEntries)
        Response.Write(keyValue(0).Replace("""", "") & " : " & keyValue(1).Replace("""", "") & "<br/>")
    Next

简而言之,我解码QueryString(“data”),替换大括号并使用逗号作为第一个拆分字符将字符串拆分为数组。然后,我们得到一个包含以下格式“id”的值的数组:“649403”。

此后,我遍历这些值,并根据分号(:)字符为每个值分割最后一次。

使用此方法,您可以动态构建和操作数据。

代码编辑

我将“:”的所有引用替换为“=”,以确保正确检索时间戳,然后根据=拆分键值。如有必要,您可以使用选择案例将值分配给值。 (显然要确保你的变量没有在select中声明,因为它不在适当的范围内供以后使用!)

If Request.QueryString("data") IsNot Nothing Then
    Dim values() As String = Request.QueryString("data").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)
        Response.Write(keyValue(0).Replace("""", "") & " : " & keyValue(1).Replace("""", "") & "<br/>")

        Select Case keyValue(0).ToLower()
            Case "id"
                Dim id As String = keyValue(1)
        End Select
    Next
End If

反思编辑

创建对象的实例,然后检索其所有属性(Dim properties()As PropertyInfo = myObj.GetType()。GetProperties())。遍历属性并设置名称等于键的值。不要忘记导入System.Reflection库。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim data As String = "%7B%22id%22:%2269403%22,%22longitude%22:%22-143.406417%22,%22latitude%22:%2232.785834%22,%22timestamp%22:%2223-10%2010:12%22%7D"
    If data IsNot Nothing Then

        Dim myObj As New MyObject
        Dim properties() As PropertyInfo = myObj.GetType().GetProperties()
        Dim values() As String = Server.UrlDecode(data).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), Nothing)
                End If
            Next
        Next

        myObj.Save()
    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

    Public Sub Save()
        'Save logic here
    End Sub
End Class