Rajesh Ramtere Rajesh Ramtere - 29 days ago 9
JSON Question

Read data from json

This is my JSON file

{
"styleMapping": {
"1": {
"zIndex": 1,
"StyleMappingCollection": {
"636404903145791477": {
"border": {
"color": "#FF000000",
"width": "Small",
"type": "Solid"
},
"background": {
"bgOption": "Image",
"thumbOption": "Pointer",
"opacity": 1.0,
"bgColor": "#FFF5F5DC",
"bgImage": "C:\Users\raj\Downloads\images\image.jpg",
}
}
}


i want to capture bgImage parameter in my javascript.

My script code is

$http.get('Data//xyz.json').then(successCallback, errorCallback);

function successCallback(response)
{
sliderCtrlPtr.sliderParams = response.data;
sliderCtrlPtr.sliderParams.height = response.data.deviceHeight;

console.log("After JSON read : ",sliderCtrlPtr.sliderParams);

}
function errorCallback(error)
{
//error code
}

sliderCtrlPtr.GetsliderStyle = function ()
{
if(sliderCtrlPtr.sliderParams != undefined)
{
var styleObj = sliderCtrlPtr.sliderParams;
canvas.color = styleObj.StyleMappingCollection.
636404903145791477.background.bgColor;
}
};
}]);
})();


i want to retrieve bgColor or bgImage parameter from my json file in my script. How can I do that?

Answer Source

I think the line should be. From the JSON provided which is not complete, I could only find this error!

sliderCtrlPtr.GetsliderStyle = function() {
    if (sliderCtrlPtr.sliderParams != undefined) {
      var styleObj = sliderCtrlPtr.sliderParams;
      canvas.color = styleObj["styleMapping"]["1"]["StyleMappingCollection"]
      ["636404903145791477"]["background"]["bgColor"];
      canvas.image = styleObj["styleMapping"]["1"]["StyleMappingCollection"]
      ["636404903145791477"]["background"]["bgImage"];
    }
}