Received this JSON object from backend services
[{"date":"2014-02-06","fontSize":14,"bgColor":"#000000","fontType":"Arial","userId":1012}]
Trying to extract and utilize the elements within the JSON object in my code, but encountering undefined
when using eval function. For example: var date_time = data.date_time;
getting undefined
for data.date_time
Tried various methods but still receiving undefined
. Seeking assistance with a solution :)
Beginner in javascript/json field, pardon the simplicity of the question ;)
Code snippet below:
var getUsersettings= "userId="+userId+"&sessionId="+sessionId;
$.ajax({
type:"POST",
url: fetchUserSettings,
async: false,
cache: false,
dataType: "json",
data: getUsersettings,
error : function(data) {
console.log("settings lost while fetching !!!!!");
},
success : function(data) {
console.log("settings fetched !!!!!");
if(data != undefined && data.length > 0){
console.log(data);
// var obj = JSON.parse(data);
var obj = JSON.stringify(data);
console.log(obj);
// obj = jQuery.parseJSON(obj);
/* var data =eval('(' +data + ')');
console.log(data);
*/
data = eval('('+obj+')');
console.log("obj : "+data);
var date_time = data.date_time;
var font_size = data.fontSize;
var bg_Color = data.bgColor;
var font_type = data.fontType;
data.userId;
// console.log("---"+date_time+"-------"+font_size+"------"+bg_Color+"----"+font_type+"------"+data.userId);
}
else{
console.log("no data abt fetching");
}
},
complete: function(xhr, textStatus) {
console.log("Status Code: " + xhr.status);
}
});