Hello.

I try to transfer from JSP to JSON.

<c:set var="json_text"> [{"TIME_STAMP": "2016-07-25 02:30:22", "COUNTER": "COUNT_ALL", "VALUE": 635 }, { "TIME_STAMP": "2016-07-25 02:30:22", "COUNTER": "COUNT_BAD", "VALUE": 1 }, { "TIME_STAMP": "2016-07-25 02:30:22", "COUNTER": "QUALITY", "VALUE": 99.84 }, { "TIME_STAMP": "2016-07-25 02:31:22", "COUNTER": "COUNT_ALL", "VALUE": 642 }, { "TIME_STAMP": "2016-07-25 02:31:22", "COUNTER": "COUNT_BAD", "VALUE": 1 }, { "TIME_STAMP": "2016-07-25 02:31:22", "COUNTER": "QUALITY", "VALUE": 99.84 }] </c:set> <script type="text/javascript"> var jsonString='<c:out value="${json_text}"/>'; var jsonObj = JSON.parse(jsonString); </script> 

Uncaught SyntaxError error reproduced: Unexpected token & in JSON at position 2

BUT if you take and copy the entire content directly into the Java script, then everything works.

  <script type="text/javascript"> var jsonString='[{"TIME_STAMP": "2016-07-25 02:30:22", "COUNTER": "COUNT_ALL", "VALUE": 635 }, { "TIME_STAMP": "2016-07-25 02:30:22", "COUNTER": "COUNT_BAD", "VALUE": 1 }, { "TIME_STAMP": "2016-07-25 02:30:22", "COUNTER": "QUALITY", "VALUE": 99.84 }, { "TIME_STAMP": "2016-07-25 02:31:22", "COUNTER": "COUNT_ALL", "VALUE": 642 }, { "TIME_STAMP": "2016-07-25 02:31:22", "COUNTER": "COUNT_BAD", "VALUE": 1 }, { "TIME_STAMP": "2016-07-25 02:31:22", "COUNTER": "QUALITY", "VALUE": 99.84 }]'; var jsonObj = JSON.parse(jsonString); </script> 

Tell me what the problem is.

ps at first thought that in var jsonString = "; just the string is transmitted, but checked in this way

 <script type="text/javascript"> var JSONFromJSTL='<c:out value="${json_text}"/>'; jsonResult.push("Вывод JSON="+JSONFromJSTL); result_json.innerHTML = jsonResult.join('<br>'); </script> 

at

 <div id="result_json"></div> 

such result is transmitted

JSON = [{"TIME_STAMP": "2016-07-25 02:30:22", "COUNTER": "COUNT_ALL", "VALUE": 635}, {"TIME_STAMP": "2016-07-25 02: 30:22 "," COUNTER ":" COUNT_BAD "," VALUE ": 1}, {" TIME_STAMP ":" 2016-07-25 02:30:22 "," COUNTER ":" QUALITY "," VALUE ": 99.84}, {"TIME_STAMP": "2016-07-25 02:31:22", "COUNTER": "COUNT_ALL", "VALUE": 642}, {"TIME_STAMP": "2016-07-25 02:31 : 22 "," COUNTER ":" COUNT_BAD "," VALUE ": 1}, {" TIME_STAMP ":" 2016-07-25 02:31:22 "," COUNTER ":" QUALITY "," VALUE ": 99.84 }]

those. exactly what is needed.

    2 answers 2

    the whole thing you were in var jsonObj = $ {json_text};

    this is how the result was achieved

     <script type="text/javascript"> var jsonObj = ${json_text}; var jsonResult = []; jsonResult.push("Вывод JSON="+'<c:out value="${json_text}"/>'); jsonResult.push("количество строк json="+jsonObj.length); result_json.innerHTML = jsonResult.join('<br>'); </script> 

      To generate json in jsp you can use the tag library http://json-taglib.sourceforge.net