public void getstreet() {
HttpServletRequest request = ServletActionContext.getRequest();
String areaid = request.getParameter("areaid");
System.out.println("-------aid--------->" +listarea);
List<Street> listStreet = houseservice.listStreet(Integer.parseInt(areaid));
JSONObject jsonArray= JSONObject.fromObject(listStreet);}
这个是我方法listStreet 就是从数据库里面的到的值我用debug调式里面不为空有值
但是我用这个方法JSONObject jsonArray= JSONObject.fromObject(listStreet);的时候报出异常
严重: Servlet.service() for servlet default threw exception
net.sf.json.JSONException: 'object' is an array. Use JSONArray instead
at net.sf.json.JSONObject.fromObject(JSONObject.java:190)
at net.sf.json.JSONObject.fromObject(JSONObject.java:154)
at org.shj.houseproject.house.action.HouseAction.getstreet(HouseAction.java:181)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:404)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:267)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:229)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:221)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.j
不知道哪里出了问题
HttpServletRequest request = ServletActionContext.getRequest();
String areaid = request.getParameter("areaid");
System.out.println("-------aid--------->" +listarea);
List<Street> listStreet = houseservice.listStreet(Integer.parseInt(areaid));
JSONObject jsonArray= JSONObject.fromObject(listStreet);}
这个是我方法listStreet 就是从数据库里面的到的值我用debug调式里面不为空有值
但是我用这个方法JSONObject jsonArray= JSONObject.fromObject(listStreet);的时候报出异常
严重: Servlet.service() for servlet default threw exception
net.sf.json.JSONException: 'object' is an array. Use JSONArray instead
at net.sf.json.JSONObject.fromObject(JSONObject.java:190)
at net.sf.json.JSONObject.fromObject(JSONObject.java:154)
at org.shj.houseproject.house.action.HouseAction.getstreet(HouseAction.java:181)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:404)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:267)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:229)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:221)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.j
不知道哪里出了问题
让你用JSONArray方法。
'object' is an array. Use JSONArray instead这句话多明显啊孩子。
让你用JSONArray方法。
让你用JSONArray方法。
alert(" ");
$("#areaid").change(function() {
var areaid = $("#areaid").val();
$.ajax({
url:'getstreet.action',
data:{"areaid":areaid},
type:"post",
success:function(data){
$("#street").empty();
var streets = jQuery.parseJSON(data);
$.each(eval(streets), function(i, street) {
$("<option value='" + street.id + "'>" + street.name + "</option>").appendTo($("#street"));
});
}
})
});
})恩 我昨天试了不行今天怎么就可以了
但是我jquery写的为什么报object is null
求解决
1. 要么jQuery.parseJSON(data)转换有问题,接收到的后台data的json字符串格式不正确,无法装换为json对象
2. 要么data没有接收到后台返回的data的json字符串。由于你后台返回的是json对象数组。所以原因该是你转换成对象数组出问题了jQuery.parseJSON(data);
这个转换出来应该是json对象而不是数组吧。在$.each回调函数里面循环转换把。。
我ACTION里面的代码怎么写
那jquery代码页怎么写啊
我是个新手
求教了
public void writerJSON(String info, HttpServletResponse response) {
setJSONHeader(response);
try {
PrintWriter out = response.getWriter();
out.write(info);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
参数info就是json字符串,就可以发送到前端
action返回值写return null.就行然后前台接收data数据,利用回调函数循环转换为对象,然后显示出来就行了
public void getstreet() throws IOException {
HttpServletRequest request = ServletActionContext.getRequest();
String areaid = request.getParameter("areaid");
System.out.println("-------aid--------->" + listarea);
List<Street> listStreet = houseservice.listStreet(Integer
.parseInt(areaid));
// JSONObject jsonArray= JSONObject.fromObject(listStreet);
HttpServletResponse response = ServletActionContext.getResponse();
PrintWriter pw = response.getWriter();
response.setCharacterEncoding("text/html;charset=utf-8");
String jsonArray = JSONArray.fromObject(listStreet).toString();
pw.write(jsonArray);这个是我方法 你看
JSP页面显示乱码
遇到乱码 要从头开始排除,是哪一步乱码、
首先看看后台jsonArray这个字符串中的中文是否乱码,确保传回去时,没有乱码。。
其次在看看你页面中接收到的data是不是乱码的,确保接收没有乱码。。
然后再看看将data转换为对象后,有没有乱码
一层一层的去排除 就可找到原因 你后台的编码方式是utf-8,所以得看看前台是什么编码。要统一
如何解决我在ACTION里面设置了编码方式为utf-8
可是传到data里面就是乱码了 都是问号
response.setCharacterEncoding("text/html;charset=utf-8");不知道你的项目有没有写编码过滤器
你找一下你哪里编码没有统一的吧或者浏览器引起的
但是jsp里解析json是string类型的如何将value 和name 分开啊
这个是我jquery代码
$.each(eval(data), function(i, street) {
$("<option value='" + i + "'>" +street + "</option>").appendTo($("#street"));
})
得到却是5,汉口大道1,6,汉口大道2
我数据库里面是5对应汉口大道1 6对应汉口大道2
求方法
我在网上找了许多方法不行
可是LZ接下来问了几个问题。就意味着我要和其他人分享这40分。
但可是LZ又继续问问题了。这个帖子会成为初学者的天堂的。
因为LZ遇到的所有问题都会出现在这个帖子里。
我后台是把集合转换成了字符串 但是我jquery不知道怎么把字符串转换回来我在网上也查了许多方法不知道怎么解决 还需要大哥们 帮帮我了
java.io.FileNotFoundException: ..\server\default\deploy\fe.war\systemLog.log (系统找不到指定的路径。)