开发者

problem with parsing Json return object

I have the following json output when i call a sample webservice

<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>

I am facing problem in parsing this result and appending it to some sample text boxes

This i s my sample html file

<!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 />
    <str开发者_如何转开发ong>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`


This should work. Change testJson like this.

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);
        }
    });
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜