anirudh reddy anirudh reddy - 1 year ago 80
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';
var responseJson = {};
var project_type_obj = JSON.parse(project_type_str);
var value = project_type_obj["value_1"];


Thanks for your answers.Please help me

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download