locked
Character set issue with json and odata RRS feed

  • Question

  • Has anyone run into the issue below.  Below the function attempts to retrieve the product standard cost field using odata.  Everything is working but I can’t get the output to the correct format.  I’ve dumped the output below as well. 

    I’ve tried changing the character set to iso-8859-1 but it still doesn’t work.  Any thoughts?  Thanks in advance for your help!

    function getStandardCost() {
                    var productidvalue = Xrm.Page.data.entity.attributes.get("productid").getValue()[0].id;
                         // alert(productidvalue);

    // Creating the Odata Endpoint
        var oDataPath = "https://myserver.com/XRMServices/2011/OrganizationData.svc";
        var retrieveReq = new XMLHttpRequest();
        var Odata = oDataPath + "/ProductSet(guid'" + productidvalue + "')?$select=StandardCost"
        retrieveReq.open("GET", Odata, true);
        retrieveReq.setRequestHeader("Accept", "application/json");
        retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        retrieveReq.onreadystatechange = function () { retrieveReqCallBack(this); };
        retrieveReq.send();
        // alert(Odata);
       }

    function retrieveReqCallBack(retrieveReq) {
        if (retrieveReq.readyState == 4 /* complete */) {
            var retrieved = this.parent.JSON.stringify(retrieveReq.responseText);
            // var retrievedStandardCost = retrieved.results[0].StandardCost;
            alert(retrieved);

         }
    }


    OUTPUT:

    "{\r\n\"d\" : {\r\n\"__metadata\": {\r\n\"uri\": \"https://myserver.com/XRMServices/2011/OrganizationData.svc/ProductSet(guid'4eb8e906-3d99-e111-a3d6-005056902e5b')\", \"type\": \"Microsoft.Crm.Sdk.Data.Services.Product\"\r\n}, \"StandardCost\": {\r\n\"__metadata\": {\r\n\"type\": \"Microsoft.Crm.Sdk.Data.Services.Money\"\r\n}, \"Value\": \"39.0000\"\r\n}\r\n}\r\n}"


    • Edited by jrh4211 Saturday, July 14, 2012 12:36 AM
    Saturday, July 14, 2012 12:33 AM

Answers

  • Thank you for the response.  That didn't work, but was able to get it to work with this:

            var retrieved = this.parent.JSON.parse(retrieveReq.responseText).d;
            var standardCost = retrieved.StandardCost.Value;

    Thanks again for the help.


    JH

    • Marked as answer by jrh4211 Saturday, July 14, 2012 8:18 PM
    Saturday, July 14, 2012 8:18 PM

All replies

  • Hello,

    Try to use

    var retrievedStandardCost = parseFloat(retrieved.results[0].StandardCost.Value);


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter

    Saturday, July 14, 2012 8:34 AM
    Moderator
  • Thank you for the response.  That didn't work, but was able to get it to work with this:

            var retrieved = this.parent.JSON.parse(retrieveReq.responseText).d;
            var standardCost = retrieved.StandardCost.Value;

    Thanks again for the help.


    JH

    • Marked as answer by jrh4211 Saturday, July 14, 2012 8:18 PM
    Saturday, July 14, 2012 8:18 PM
  • You're errantly attempting to use JSON.stringify to process the JSON response.  JSON.stringify is used to turn an object into a JSON string:

    var obj = {
      prop1: value1,
      prop2: value2
    };
    
    var jsonString = JSON.stringify(obj);

    To parse the returned JSON response string, use JSON.parse():

    var obj = JSON.parse(retrieveReq.responseText).d;
    
    var retrievedStandardCost = obj.StandardCost.Value;


    --pogo (pat) @ pogo69.wordpress.com


    • Edited by pogo69 Saturday, July 14, 2012 8:27 PM
    Saturday, July 14, 2012 8:27 PM