繁体   English   中英

使用Javascript使用JSON从字符串获取数据

[英]Getting the data from a string using JSON using Javascript

我需要您的帮助:我创建了一个Web服务,单击url后返回一个干净的字符串:

{
    "PersonID": 125,
    "Title": "Security Officer",
    "Company": "TSA",
    "CellNum": "423-915-3224",
    "EmergencyPhone": "",
    "Email": ""
}

如何使用JSON提取字符串并获取数据?

我的Web服务:

 <OperationContract()>
    <WebGet(ResponseFormat:=WebMessageFormat.Json, UriTemplate:="/getPersonInfo/?personID={personID}&companyCode={companyCode}", BodyStyle:=WebMessageBodyStyle.Bare)>
    Public Function getPersonInfo(ByVal personID As String, ByVal companyCode As String) As Stream
        Dim dba As New DBAccess
        Dim person As New PersonInfo
        Dim m_SelPerson As String = String.Empty
        Dim ds As DataSet = dba.GetPersonInfo(personID, companyCode)
        If Not ds Is Nothing Then
            Dim dr As DataRow = ds.Tables(0).Rows(0)
            person = New PersonInfo
            person.PersonID = Convert.ToInt32(dr("PersonID"))
            person.Company = dr("Company")
            person.Title = dr("Title")
            person.CellNum = dr("CellNum")
            person.EmergencyPhone = dr("EmergencyPhone")
            person.Email = dr("Email")
            Dim oSerilzer As New System.Web.Script.Serialization.JavaScriptSerializer
            m_SelPerson = oSerilzer.Serialize(person)
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8"
        End If
        Return New MemoryStream(Encoding.UTF8.GetBytes(m_SelPerson))
    End Function

JS功能:

function getPersonInfo(personID) {
        var json = '"http://122.123.1.118/GSWS/Service1.svc/REST/getPersonInfo/?personid=125&companycode=TSA&sensor=false"';
        obj = JSON.parse(json);
        alert(obj);
    }
<http>
<script src='http://code.jquery.com/jquery-1.11.1.min.js'></script>
<body>

<script>
    var url = "http://122.123.1.118/GSWS/Service1.svc/REST/getPersonInfo/?personid=125&companycode=TSA&sensor=false";
    $.getJSON( url, function( data ) {
        alert(data.PersonID);
    });
</script>

</body>
</html>

暂无
暂无

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

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