java JSON object in javascript

1k Views Asked by At

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"} 
1

There are 1 best solutions below

0
On BEST ANSWER

In the JavaScript, you need to parse the json instead of using JSON.stringify it should be JSON.parse(testJSONData), then you will be use testJSONDataObj.KEY1.