Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

jquery, each statement

I just need to know how to get the list of shiptoName. I know I can use each statement but no idea how to deal with "\".

Please helps.

Thanks

RETURN response from wcf
{
    "PackageKey": null,
    "PackageNo": 1,
    "TotalPackages": 1,
    "Status": "Ok",
    "Data": "{\"Shiptos\":[{\"ShiptoNo\":\"00\",\"ShiptoName\":\"DATA SALES CO. INC.\",\"ContactName\":\"\",\"ContactTitle\":\"\",\"Address_1\":\"JOHN SMITHS\",\"Address_2\":\"3450 WEST\",\"Address_3\":\"\",\"City\":\"BURN\",\"State\":\"MN\",\"Zip\":\"52337\",\"Phone\":\"888-888-8838\",\"CellPhone\":\"\",\"Fax\":\"\",\"Email\":\"\"}],\"DealerNo\":\"305033\",\"DealerName\":\"DATA. INC.\",\"ContactTitle\":\"\",\"ContactName\":\"\",\"Address_1\":\"JOHN SMITHS\",\"Address_2\":\"3450 WEST PARKWAY\",\"Address_3\":\"\",\"City\":\"BURNSVILLE\",\"State\":\"MN\",\"Zip\":\"55337\",\"Phone\":\"888-890-8838\",\"CellPhone\":\"\",\"Fax\":\"\",\"Email\":\"\"}"
}


how it is consumed
<script type="text/javascript">
    function GetDealerShiptoList() {
        var GetDealerShiptoListJSon = "http://clientaccesstest.abc.com/wcf/orders/RestService/QuoteService/GetDealerShiptoListJSon(066,305082)";
        $(document).ready(function () {
            $.ajax({
                crossDomain: true,
                cache: false,
                type: "GET",
                async: false,
                dataType: "json",
                contentType: 'text/plain',

                url: GetDealerShiptoListJSon,
                success: function (data) {
                     
                     
                },
                error: function (xhr, status, error) {
                    alert(xhr.responseText + " : error SelectDealerShipTo");
                }
            });
        });
    }
</script>
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

How about this?
...
 success: function (data) {
       var dataParsed = data.replace('\\"', "'");
       dataParsed = JSON.parse(dataParsed);
       var ShipToName =  dataParsed.Shiptos[0].ShiptoName;                    
 },
...

Open in new window

The value you seek would be in the variable ShipToName.
Avatar of ITsolutionWizard

ASKER

Does it return on or more rows?
Adjustment to @Tom's suggestion:
...
 success: function (data) {
       var dataParsed = data.replace('\\"', "'");
       dataParsed = JSON.parse(dataParsed);
       
       // because "Data" property of main JSON object has value wrapped in double quote:  "Data": "{...}"
       // so "Data" property is considered as a string type instead of nested JSON object.
       // we need to parse the value of "Data" property to JSON object before we can access "Shiptos"
       var shiptosParsed = JSON.parse(dataParsed.Data);
       var ShipToName =  shiptosParsed.Shiptos[0].ShiptoName;                    
 },
...

Open in new window

It will.be.a.list so.expect.a.drop.down
After parsing inner "Data" property of main JSON object, "Shiptos" property is an array of JSON object. So you can just loop through all elements in that array and collect "ShiptoName" collection, and then fill them in your dropdown.
You may want to check with the creator of your service. It seems odd that they would be encoding the value of the "Data" property as a string when it appears to be JSON. I'm wondering if something got serialized incorrectly by the service.
It does not work at all.
the service format is actually below

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
{
    "PackageKey": null,
    "PackageNo": 1,
    "TotalPackages": 1,
    "Status": "Ok",
    "Data": "{\"Shiptos\":[{\"ShiptoNo\":\"00\",\"ShiptoName\":\"DATA SALES CO. INC.\",\"ContactName\":\"\",\"ContactTitle\":\"\",\"Address_1\":\"JOHN SMITHS\",\"Address_2\":\"3450 WEST\",\"Address_3\":\"\",\"City\":\"BURN\",\"State\":\"MN\",\"Zip\":\"52337\",\"Phone\":\"888-888-8838\",\"CellPhone\":\"\",\"Fax\":\"\",\"Email\":\"\"}],\"DealerNo\":\"305033\",\"DealerName\":\"DATA. INC.\",\"ContactTitle\":\"\",\"ContactName\":\"\",\"Address_1\":\"JOHN SMITHS\",\"Address_2\":\"3450 WEST PARKWAY\",\"Address_3\":\"\",\"City\":\"BURNSVILLE\",\"State\":\"MN\",\"Zip\":\"55337\",\"Phone\":\"888-890-8838\",\"CellPhone\":\"\",\"Fax\":\"\",\"Email\":\"\"}"
}

</string>
I tried below using your codes. still not working.

<script type="text/javascript">
        function GetDealerShiptoList() {
            var GetDealerShiptoListJSon = ""http://clientaccesstest.abc.com/wcf/orders/RestService/QuoteService/GetDealerShiptoListJSon(066,305082)";
            $(document).ready(function () {
                $.ajax({
                    crossDomain: true,
                    cache: false,
                    type: "GET",
                    async: false,
                    dataType: "text",
                    contentType: 'text/plain',
                    url: GetDealerShiptoListJSon,
                    success: function (data) {
                        data = data.replace('<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">', '');
                        data = data.replace('</string>', '');
                        data = data.replace('\\"', "'");
                        var finaldata = data;
                        dataParsed = JSON.parse(finaldata);
                        var shiptosParsed = JSON.parse(dataParsed.Data);
                        var ShipToName = shiptosParsed.Shiptos[0].ShiptoName;
                        alert(ShipToName);
                    },
                    error: function (xhr, status, error) {
                        // alert(xhr.responseText + " : error SelectDealerShipTo");
                    }
                });
            });
        }
</script>
I finally codes it below. and need some helps. I am able to get the value for the city

BURNSVILLE

but it supposes to return BURNSVILLE 2 times. Can you show me how to loop after the following statement?

var shiptosParsed = JSON.parse(dataParsed.Data);
var ShipToName = shiptosParsed.Shiptos[0].City;

Thanks,



<script type="text/javascript">
        function GetDealerShiptoList() {
            var GetDealerShiptoListJSon = "http://clientaccesstest.kenwoodusa.com/wcf/proquotes/RestService/ProQuoteServiceJVC/GetDealerShiptoListJSon(066,305082)";
            $(document).ready(function () {
                $.ajax({
                    crossDomain: true,
                    cache: false,
                    type: "GET",
                    async: false,
                    dataType: "text",
                    contentType: 'text/plain',
                    url: GetDealerShiptoListJSon,
                    success: function (data) {
                        data = data.replace('<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">', '');
                        data = data.replace('</string>', '');
                        var finaldata = data;
                        var dataParsed = JSON.parse(finaldata);
                        var shiptosParsed = JSON.parse(dataParsed.Data);
                        var ShipToName = shiptosParsed.Shiptos[0].City;
                        $("#result").append(ShipToName);
                    },
                    error: function (xhr, status, error) {
                        // alert(xhr.responseText + " : error SelectDealerShipTo");
                    }
                });
            });
        }
</script>
I try with following code, and it works just fine:
        $.ajax({
                url: 'http://clientaccesstest.kenwoodusa.com/wcf/orders/RestService/ProQuoteServiceJVC/GetDealerShiptoListJSon(066,305082)',
                type: "GET",
                dataType: "json",
                crossDomain: true,
		contentType: "text/plain",
                success: function (msg) {
		    var mainJSONObject = $.parseJSON(msg);
		    var shiptosParsed = $.parseJSON(mainJSONObject.Data);
		    alert(shiptosParsed.Shiptos[0].ShiptoName);
		    var shiptoNames = [];
		    $(shiptosParsed.Shiptos).each(function() { shiptoNames.push(this.ShiptoName); });
		    alert(shiptoNames.length);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert('failed: ' + textStatus + ' - ' + errorThrown + ' - ' + jqXHR.responseXml);
                },
          });

Open in new window

it is not working. and please use my sample. I want to get city and which is supposed to return twice.
ASKER CERTIFIED SOLUTION
Avatar of Duy Pham
Duy Pham
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I rewrote the codes and it works in Chrome/Firefox but not working in IE. No idea...any helps?
<script type="text/javascript">
        function GetDealerShiptoList() {
        var companyID = "066";
        var dealerNo = "315158";
        var GetDealerShiptoListJSon = "http://....GetDealerShiptoListJSon";
        $(document).ready(function () {
            $.ajax({
                crossDomain: true,
                cache: false,
                type: "GET",
                async: false,
                dataType: "text",
                contentType: "text/plain",
                url: GetDealerShiptoListJSon + encodeURIComponent("(" + companyID + "," + dealerNo + ")"),
                success: function (data) {
                    data = data.replace('<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">','');
                    data = data.replace('</string>','');
                    var finalData = data;
                    var dataParsed = JSON.parse(finalData);
                    var shiptosParsed = JSON.parse(dataParsed.Data);                    
                    for (i = 0; i <= shiptosParsed.Shiptos.length; i++) {
                        var cityName = shiptosParsed.Shiptos[i].City;
                        var stateName = shiptosParsed.Shiptos[i].State;
                        var shipToName = shiptosParsed.Shiptos[i].ShiptoName;
                        $("#SelectDealerShipTo").append("<option>" + shipToName + ", " + cityName + ", " + stateName + "</option>");
                    }
                },
                error: function (xhr, status, error) {
                    alert(xhr.responseText + " : error SelectDealerShipTo");
                }
            });
        });
    }
 </script>

Open in new window

What error did you get in IE (using F12 - Developer Tools)?