extjs 使用json传数有关问题
extjs 使用json传数问题 - Web 开发 / Ajax
我在做一个树形菜单的时候需要将查询的数据用json封装代码如下:方案1运行的时候系统会停住什么也不报,而采用拼接字符串的方案2则可以运行成功,请高手指明什么原因,谢谢
方案1、
try {
response.setContentType("text/html;CHARSET=gb2312");
PrintWriter out = response.getWriter();
JSONObject allvalue = new JSONObject();
JSONArray menu = new JSONArray();
JSONObject temp = new JSONObject();
temp.put("id", "1");
temp.put("text", "ak47");
temp.put("leaf", "true");
menu.add(temp);
allvalue.put("menu", menu);
out.print(allvalue);
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
方案2:
try {
response.setContentType("text/html;CHARSET=gb2312");
PrintWriter out = response.getWriter();
String allvalue="{'menu':[{'id':'1','text':'ak47','leaf':'false'}]}";
out.print(allvalue);
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
------解决方案--------------------
我在做一个树形菜单的时候需要将查询的数据用json封装代码如下:方案1运行的时候系统会停住什么也不报,而采用拼接字符串的方案2则可以运行成功,请高手指明什么原因,谢谢
方案1、
try {
response.setContentType("text/html;CHARSET=gb2312");
PrintWriter out = response.getWriter();
JSONObject allvalue = new JSONObject();
JSONArray menu = new JSONArray();
JSONObject temp = new JSONObject();
temp.put("id", "1");
temp.put("text", "ak47");
temp.put("leaf", "true");
menu.add(temp);
allvalue.put("menu", menu);
out.print(allvalue);
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
方案2:
try {
response.setContentType("text/html;CHARSET=gb2312");
PrintWriter out = response.getWriter();
String allvalue="{'menu':[{'id':'1','text':'ak47','leaf':'false'}]}";
out.print(allvalue);
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
------解决方案--------------------
- CSS code
JSONObject allvalue = new JSONObject(); JSONArray menu = new JSONArray(); JSONObject temp = new JSONObject(); temp.put("id", "1"); temp.put("text", "ak47"); temp.put("leaf", "true"); menu.add(temp); allvalue.put("menu", menu); out.print(allvalue); 你的错误原因是只把数据封装到JSONObject中,并没有转换。 JSONObject 好像有一个方法:formatString(); allvalue.formatString();//不知道有没有记错,返回是String类型
------解决方案--------------------
out.print(allvalue);这里不会输出东西
out.print(allvalue.toString()); 这样才会输出json的内容
------解决方案--------------------
------解决方案--------------------
allvalue.put("menu", menu);
out.print(allvalue);
out.close();
你这个输出的是和 方案二 的json格式
"{'menu':[{'id':'1','text':'ak47','leaf':'false'}]}"
一样的东西吗