1

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 />
    <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`
4
  • Can you give us the javascript you are using? This is validating as valid JSON from JSONLint Commented Jun 4, 2011 at 9:46
  • What sort of problems? You could use jQuery's getJSON method . This allows you to retrieve JSON from a webservice call. Commented Jun 4, 2011 at 10:10
  • 1
    Might sound weird, but just to be sure.. :) The response doesn't actually return also the <string> and </string>, does it? Commented Jun 4, 2011 at 10:15
  • You don't need JSON.parse if you are calling with a dataType of JSON set Commented Jun 4, 2011 at 11:12

1 Answer 1

1

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);
        }
    });
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.