解析Json返回对象的问题

时间:2011-06-04 09:40:24

标签: jquery asp.net json

当我调用示例webservice时,我有以下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`

1 个答案:

答案 0 :(得分: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);
        }
    });
}