如何在javascript中使用wcf url中的JSON数据

How to use JSON data from wcf url in javascript

本文关键字:url wcf 中的 JSON 数据 javascript      更新时间:2023-09-26

我正在尝试从javascript中的WCF服务url接收JSON数据。

我使用了以下javascript代码:

$(function(){
$("input:button").click(function() {
   $.ajax({
      dataType: 'json',
      type: "GET",
      url: "http://192.168.1.109/EIM/EIS.svc/json/getLatestLocation/Mobile/Mobile/1001",
      success: function (data) {
          var innerHtml = "";
          document.getElementById('test').innerHTML=data;
                 //'test' is ID of <div>
          document.getElementById('testlab').innerHTML=data.getLatestLocationResult.status.code;
                //'testlab' is ID of <label>
          $("#test").html(innerHtml);
          alert("JSON DATA");
          alert(data.getLatestLocationResult.status.code);
          }
        });
    });
 });

来自url的JSON数据是:

 {"getLatestLocationResult":{"status":{"code":"00","description":"Request
 OK"},"reason":{"code":"000","description":"Request
 OK"},"PersonDetails":{"PersonName":"Santosh Kumar
 Sahu","PersonNumber":"1001","Identification":"Pan
 Card","IdentificationNumber":"P567","ContactNo":"987654","Gender":"Male"},"location":[{"dateandtime":"7'/17'/2013
 10:45:20   AM","latitude":"19.0469536","longitude":"72.9112502","address":"ND","city":"ND","state":"ND","country":"ND"}]}}

请给我一个使用这些数据的建议。

谢谢。

您应该将数据eval -否则它只是一个字符串。

obj = eval('(' + data + ')');

现在你可以使用obj作为一个合法的JS对象,并访问它的成员。这里是小提琴评估您的data字符串,并访问结果对象成员http://jsfiddle.net/M4BK5/