繁体   English   中英

通过javascript从json字符串中提取值

[英]extract values from json string via javascript

我有一个Web服务,该服务以JSON返回结果。在Javascript中,我想遍历这些结果,以便将其显示给用户。

我定义了以下类:

Public Class PointsOfInterest
    Public Property hotspots() As List(Of Hotspot)
        Get
            Return m_hotspots
        End Get
        Set(value As List(Of Hotspot))
            m_hotspots = value
        End Set
    End Property
    Private m_hotspots As List(Of Hotspot)
    Public Property errorString() As String
        Get
            Return m_errorString
        End Get
        Set(value As String)
            m_errorString = value
        End Set
    End Property
    Private m_errorString As String
    Public Property errorCode() As Integer
        Get
            Return m_errorCode
        End Get
        Set(value As Integer)
            m_errorCode = value
        End Set
    End Property
    Private m_errorCode As Integer
End Class

Public Class Hotspot
    Private m_id As String
    Public Property id() As String
        Get
            Return m_id
        End Get
        Set(value As String)
            m_id = value
        End Set
    End Property

    Public Property anchor() As anchor
        Get
            Return m_anchor
        End Get
        Set(value As anchor)
            m_anchor = value
        End Set
    End Property
    Private m_anchor As anchor


    Public Property text() As textprop
        Get
            Return _text
        End Get
        Set(value As textprop)
            _text = value
        End Set
    End Property
    Private _text As textprop


    Public Property imageURL() As String
        Get
            Return _imageURL
        End Get
        Set(value As String)
            _imageURL = value
        End Set
    End Property
    Private _imageURL As String

    Public Property actions() As List(Of action)
        Get
            Return _actions
        End Get
        Set(value As List(Of action))
            _actions = value
        End Set
    End Property
    Private _actions As List(Of action)

End Class

Public Class anchor
    Public Property geolocation() As geoloc
        Get
            Return _geoloc
        End Get
        Set(value As geoloc)
            _geoloc = value
        End Set
    End Property
    Private _geoloc As geoloc

End Class

Public Class textprop
    Public Property title() As String
        Get
            Return _title
        End Get
        Set(value As String)
            _title = value
        End Set
    End Property
    Private _title As String
    Public Property description() As String
        Get
            Return _description
        End Get
        Set(value As String)
            _description = value
        End Set
    End Property
    Private _description As String
    Public Property footnote() As String
        Get
            Return _footnote
        End Get
        Set(value As String)
            _footnote = value
        End Set
    End Property
    Private _footnote As String

End Class


Public Class geoloc
    Public Property lat() As String
        Get
            Return _lat
        End Get
        Set(value As String)
            _lat = value
        End Set
    End Property
    Private _lat As String
    Public Property lon() As String
        Get
            Return _lon
        End Get
        Set(value As String)
            _lon = value
        End Set
    End Property
    Private _lon As String
End Class

Public Class action
    Public Property uri() As String
        Get
            Return _uri
        End Get
        Set(value As String)
            _uri = value
        End Set
    End Property
    Private _uri As String

    Public Property label() As String
        Get
            Return _label
        End Get
        Set(value As String)
            _label = value
        End Set
    End Property
    Private _label As String
    Public Property contentType() As String
        Get
            Return _contentType
        End Get
        Set(value As String)
            _contentType = value
        End Set
    End Property
    Private _contentType As String
    Public Property method() As String
        Get
            Return _method
        End Get
        Set(value As String)
            _method = value
        End Set
    End Property
    Private _method As String
    Public Property activityType() As Integer
        Get
            Return _activityType
        End Get
        Set(value As Integer)
            _activityType = value
        End Set
    End Property
    Private _activityType As Integer
    Public Property showActivity() As Boolean
        Get
            Return _showActivity
        End Get
        Set(value As Boolean)
            _showActivity = value
        End Set
    End Property
    Private _showActivity As Boolean
    Public Property activityMessage() As String
        Get
            Return _activityMessage
        End Get
        Set(value As String)
            _activityMessage = value
        End Set
    End Property
    Private _activityMessage As String

End Class   

我通过JSON通过网络服务返回了这些信息: http ://www.wunderwedding.com/weddingservice.svc/api/?t = 1&cid = 1&pid=6&lat=52&lng =5& d=10000&city=nijmegen&field1 =0& field2=0&field3 =0& field4=0&hasphoto = 0&hasvideo = 0&minrating = 0&lang = nl

现在,例如,如果我想读取第三个结果的tel属性(在本例中为0348551532),我将如何处理? 就像XSLT一样,我可以在其中使用表达式进行选择吗?

在JavaScript中,我假设您使用支持AJAX的jQuery或ExtJS之类的东西。 一旦在客户端获得了JSON,就可以解码Web服务结果(如果在没有特殊工具包的情况下读取JSON,则可以使用Ext.decode或eval(“”))。

JSON解码后,就可以在JavaScript中使用。

例:

var json = [Function that decodes JSON text];
var uriInfo = json.hotspots[2].actions[1].uri;

如果您使用的是FireFox,则可以安装一个名为FireBug的插件。 这将使您能够完成此类事情。 使用该工具,您可以调用console.dir(json);。 在JavaScript中,并查看javascript等所看到的整个结构。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM