json中存整形数值,前端返回为空

实现目标:

在servlet中将整型数值转为json,然后在前端获取

问题描述:

前端获取为空,显示为:console.log打印为:{}

解决办法:

在servlet中将数值转为json格式再进行传输

如下:

int res=1;
String jsonres="{"res":"+res+"}"; response.getWriter().write(JSONObject.fromObject(jsonres).toString());

如此前端即可通过key(例子中为res)获取

原文地址:https://www.cnblogs.com/StevenZheng/p/10810473.html