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>
LVL 1
ITsolutionWizardAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom BeckCommented:
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.
ITsolutionWizardAuthor Commented:
Does it return on or more rows?
Duy PhamFreelance IT ConsultantCommented:
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

OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

ITsolutionWizardAuthor Commented:
It will.be.a.list so.expect.a.drop.down
Duy PhamFreelance IT ConsultantCommented:
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.
käµfm³d 👽Commented:
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.
ITsolutionWizardAuthor Commented:
It does not work at all.
ITsolutionWizardAuthor Commented:
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>
ITsolutionWizardAuthor Commented:
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>
ITsolutionWizardAuthor Commented:
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>
Duy PhamFreelance IT ConsultantCommented:
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

ITsolutionWizardAuthor Commented:
it is not working. and please use my sample. I want to get city and which is supposed to return twice.
Duy PhamFreelance IT ConsultantCommented:
@ITsolutionWizard:  You need to be more specific about what is not working, otherwise it will be very hard for everyone to help you.

We do call your WCF REST Service, so data is yours, and in returned data, there is only one element in Shiptos array, hence you only get one BURNSVILLE City.

Maybe you misunderstood with the City of Dealer info, not in the Shiptos. And if that's the case you might want to do:
$.ajax({
    ....
    success: function (msg) {
        var mainJSONObject = $.parseJSON(msg);
        var shiptosParsed = $.parseJSON(mainJSONObject.Data);
		    
        var allCities = [];

        // collect all Cities from Shiptos
        $(shiptosParsed.Shiptos).each(function() { allCities.push(this.City); });

        // collect Dealer's City
        allCities.push(shiptosParsed.City);

        alert(allCities.length); // should show 2 here
    }
    ....
});

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ITsolutionWizardAuthor Commented:
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

Duy PhamFreelance IT ConsultantCommented:
What error did you get in IE (using F12 - Developer Tools)?
ITsolutionWizardAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.