2011-06-04 134 views
1

我有我的呼喚以下JSON輸出的樣本Web服務問題解析JSON返回對象

<string>[{"Name":"Ajay Singh","Company":"Birlasoft Ltd.","Address":"LosAngeles California","Phone":"1204675","Country":"US"},{"Name":"Ajay Singh","Company":"Birlasoft Ltd.","Address":"D-195 Sector Noida","Phone":"1204675","Country":"India"}]</string> 

我在分析這一結果,並追加到一些示例文本框

這是面臨的問題我的示例html文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title></title> 

    <script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script> 
    <script type="text/javascript" src="Scripts/Json2.js"></script> 
    <script type="text/javascript"> 
     function testJson() { 
      $.ajax({ 
       type: "POST", 
       url: "JsonWebService.asmx/TestJSON", 
       data: "{}", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (msg) { 
             $("#jsonResponse").html(msg); 
        var data = JSON.parse(msg); 
        alert(data); 
             var t = "<table border=1> <tr>" + 
              "<td> <strong>Name</strong></td> <td> " + 
              "<strong>Company</strong></td> <td> " + 
              "<strong>Address</strong></td> <td> " + 
              "<strong>Phone</strong></td> <td> " + 
              "<strong>Country</strong></td> </tr> "; 
             jQuery.each(data, function (rec) { 
              t = t + " <tr> <td> " + this.Name + "</td> <td> " + 
               this.Company + "</td> <td> " + this.Address + 
               "</td> <td> " + this.Phone + "</td> <td> " + this.Country + "</td> </tr> "; 
        var t; 
        jQuery.each(data, function (rec) { 
         t = this.Name; 
         alert(t); 
               +" " + 
                this.Company + "</td> <td> " + this.Address + 
                 "</td> <td> " + this.Phone + "</td> <td> " + this.Country + "</td> </tr> "; 
        }); 

        t = t + " </table> "; 
        $("#jsonDiv").html(t); 
       }, 
       error : function (msg) { 

       } 

      }); 
     }; 


     function testXml() { 
      $.ajax({ 
       type: "POST", 
       url: "JsonWebService.asmx/TestXML", 
       data: "{}", 
       contentType: "application/json; charset=utf-8", 
       dataType: "xml", 
       success: function (msg) { 
        //var data = JSON.parse(msg); 
        alert(msg); 
       } 
      }); 
     }; 
    </script> 

</head> 
<body> 
    <p> 
     <input id="testjson" type="button" value="Test JSON Call" onclick="testJson()" /> 
     <input id="testxml" type="button" value="Test XML Call" onclick="testXml()" /> 

    </p> 
    <br /> 
    <strong>Message Response</strong> 
    <br /> 
    <div id="jsonResponse" style="display:block;"></div> 
    <br /> 
    <strong>Processed Result</strong> 
    <br /> 
    <div id="jsonDiv" style="display:block;"></div> 
</body> 
</html>`enter code here` 
+0

你能給我們提供你正在使用的JavaScript嗎?這是從[JSONLint](http://jsonlint.com/)驗證爲有效的JSON – 2011-06-04 09:46:02

+1

可能聽起來很奇怪,但只是要確定.. :)響應實際上不會返回,是嗎? – TweeZz 2011-06-04 10:15:54

+0

如果使用設置了JSON的dataType調用,則不需要'JSON.parse' – 2011-06-04 11:12:54

回答

1

這應該有效。像這樣改變testJson

function testJson() { 
    $.ajax({ 
     type: "POST", 
     url: "JsonWebService.asmx/TestJSON", 
     data: "{}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (msg) { 
      msg = msg.hasOwnProperty("d") ? msg.d : msg; 
      $("#jsonResponse").html(msg); 
      var data = JSON.parse(msg); 

      var t = "<table border=1> <tr>" + 
        "<td> <strong>Name</strong></td> <td> " + 
        "<strong>Company</strong></td> <td> " + 
        "<strong>Address</strong></td> <td> " + 
        "<strong>Phone</strong></td> <td> " + 
        "<strong>Country</strong></td> </tr> "; 

      jQuery.each(data, function (rec) { 
       t += " <tr> <td> " + this.Name + "</td> <td> " 
         + this.Company + "</td> <td> " 
         + this.Address + "</td> <td> " 
         + this.Phone + "</td> <td> " 
         + this.Country + "</td> </tr> "; 
      }); 

      t += " </table> "; 

      $("#jsonDiv").html(t); 
     }, 
     error: function (xhr, status, error) { 
      alert(xhr.statusText); 
     } 
    }); 
}