I have java JSON object, like shown in the code, I am assigning this java JSON object to a javascript variable. I am using Dojo v1.8, used the stringify method on this javascript variable, I printed this javascript variable, in the console I can see the data in it. But when I access the property in it, it is not returning anything (testJSONData.KEY1)
<%
JSONObject myJSON = request.getJSON();
%>
<script type="text/javascript">
var testJSONData = <%=myJSON %>;
testJSONDataObj = JSON.stringify(testJSONData);
console.log("testJSONDataObj details::"+ testJSONDataObj);
console.log("testJSONData id::"+ testJSONData.KEY1);
</script>
console:
testJSONDataObj details::{"KEY1":"value1","KEY2":"value2"}
In the JavaScript, you need to parse the
json
instead of usingJSON.stringify
it should beJSON.parse(testJSONData)
, then you will be usetestJSONDataObj.KEY1
.