2018년 3월 15일 목요일

JAVA DB Data-> to change JSON -> convey to javascript

import net.sf.json.JSONArray;

java

List<HashMap<String, Object>> list = testService.selectInfo(searchVO);

JSONArray jsonArray = JSONArray.fromObject(list);


model.addAttribute("jsonArray", jsonArray);

javascript
console.log("jsonArray %o2 ", '${jsonArray}');


description

list JSONArray.formObject before parse Data

  console.log Data->value "[{DT=20180312, M50=4.44, M30=9.19}]"
  
list JSONArray.formObject after parse Data ->double quotation marks are added to the string
  console.log Data->value "[{"BRD_DT":"20180312","M_50":4.44,"M_30":9.19}]"
    
if json value dont' have double quotation marks , they are not recognized as json data

----
Problem 

if results have some special letters, Javascript dose not work properly.
ex) 'test''tt'
  ->In this case, All column remove some special letter.
ex)
    <c:forEach items="${list}" var="item" varStatus="status">
         var data = {};
         data.RANKNUM = "${item.title}";  //Included quotation ->error
         data.BRD_STD_DT = "${item.BRD_STD_DT}";
    </c:forEach>
SO it is not a good solution.
I recommend to use ajax json .


var result = JSON.parse(data.responseText);
-->This will solve atll the special characters.


Java source Example

JSONObject rv = new JSONObject();
rv.put("paging", pageVO);rv.put("info", service.getInfo(pageVO));
return new ResponseVO(rv, new ResultVO("success", "성공"));
ResponeVO.java


public class ResponseVO {

   private ResultVO resultInfo;   private Object resultContent;      public ResponseVO() {
      //Constructor   }
   
   public ResponseVO(ResultVO resultInfo) {
      this.resultInfo = resultInfo;   }
   
   public ResponseVO(Object resultContent, ResultVO resultInfo) {
      this.resultInfo = resultInfo;      this.resultContent = resultContent;   }
   
   public ResultVO getResultInfo() {
      return resultInfo;   }

   public void setResultInfo(ResultVO resultInfo) {
      this.resultInfo = resultInfo;   }

   public Object getResultContent() {
      return resultContent;   }

   public void setResultContent(Object resultContent) {
      this.resultContent = resultContent;   }

}


댓글 없음:

댓글 쓰기