anirudh reddy anirudh reddy - 4 months ago 8
JSON Question

Unable to get the value from json object in javascript

unable to get the value_1 from dataObject.Showing undefined.

var errorMessage;
var dataObject ={"project_type":"{\"value_1\":\"Ground Mount\"}"};
var project_type_str = dataObject['project_type'];
project_type_str = JSON.stringify(project_type_str);
if (project_type_str != null && project_type_str.length != 0) {
errorMessage = '';
} else {
errorMessage = 'Please select a project type';
}
alert(project_type_str);
var responseJson = {};
var project_type_obj = JSON.parse(project_type_str);
alert(project_type_obj);
var value = project_type_obj["value_1"];

alert(value);


Thanks for your answers.Please help me

Answer

project_type_str is already a string, so no need to JSON.stringify it. The code should work fine if you remove the line

Remove this line

project_type_str = JSON.stringify(project_type_str);

A comparison for your better understandability

With original code

enter image description here

With the line removed

enter image description here