String xx = [{open=true, name=3, ename=3,nodes:[{open=false, name=5599, ename=30}, {open=false, name=刘, ename=27}, {open=false, name=系统管理, ename=5}, {open=false, name=aaa管理, ename=2}, {open=false, name=审核评定, ename=1}, {open=false, name=bbb排行, ename=4}]},
{open=true, name=大源系统, ename=2},
{open=true, name=系统2, ename=1}]; 上面是类里输出的字符串如何转json
{open=true, name=大源系统, ename=2},
{open=true, name=系统2, ename=1}]; 上面是类里输出的字符串如何转json
解决方案 »
- tomcat7对mysql 驱动的支持
- 小弟初学java 对jsp不熟 有个问题困扰多时 自己还是无法解决 希望各位高手出手相助
- springguid 一书源码哪里下载?
- 关于hibernate配置文件中,hibernate.hbm2ddl.auto值的设置?
- 图片显示问题 请高手指点啊!!!
- 高手过来 看一下SSH+DWR
- struts2的struts2.1有何区别
- 求助:jsp怎么来改变WORD域变量结果的值?
- HTTP Status 500 错误 从书上下载的代码可是就是连不上数据库
- tomcat启动很慢
- 关于checkbox和radio的值 疑惑
- javamail发送html格式邮件,qq邮箱图片显示问题
http://json-lib.sourceforge.net
JSONArray json = JSONArray.fromObject(xx);
//JSONArray json = JSONArray.fromObject(modulelist);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
response.setContentType("text/plain");
response.getWriter().println(json ); 为什么这样写不对呢JSONArray json = JSONArray.fromObject(xx);
JSONArray json = JSONArray.fromObject(xx);
然后,转成单个对象的话
遍历JSONArray
会用到:JSONObject job = json.getBean();
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
response.setContentType("text/plain");
response.getWriter().println(json );
传到前台去,那位知道?
是org.json吗?
import net.sf.json.JSONObject;
import java.lang.reflect.Method;
import java.util.List;public class JsonUtil {
/**
* @param page 当前页数
* @param total 记录总条数
* @param list 符合要求的记录列表。
* @return 返回符合Flexigred格式要求的json字符串。
* @throws Exception
*/
public static String getJson(String page,String total,List list)throws Exception{
StringBuffer sb = new StringBuffer();
sb.append("{\"total\":"+total+",\"page\":\""+page+"\",\"rows\":");
sb.append(list2Json(list));
sb.append("}");
return sb.toString();
}
/**
* @param list 符合要求的记录列表。
* @return 返回符合Flexigred格式要求的json字符串。
* @throws Exception
*/
public static String getJson(List list)throws Exception{
StringBuffer sb = new StringBuffer();
sb.append("{\"rows\":");
sb.append(list2Json(list));
sb.append("}");
return sb.toString();
}
/**
* @param list 对象列表。
* @return
* @throws Exception
*/
private static String list2Json(List<Object> list)throws Exception{
if(list.size() > 0){
StringBuffer sb = new StringBuffer("[");
for (int i = 0; i < list.size(); i++) {
sb.append(obj2Json(list.get(i))+",");
}
sb.delete(sb.length()-1, sb.length());
sb.append("]");
return sb.toString();
}else{
return "[]";
}
}
/**
* @param o 对象,只支持基本类型和String,自定类型请勿使用。
* @return
* @throws Exception
*/
private static String obj2Json(Object o)throws Exception{
Object[] obj = getObjectValue(o);
StringBuffer sb = new StringBuffer("{");
sb.append("\"id\":\""+obj[0]+"\",\"cell\":[");
for (int i = 0; i < obj.length; i++) {
if(obj[i] instanceof String){
sb.append("\""+obj[i]+"\",");
}else if(obj[i] instanceof Character){
sb.append("\""+obj[i]+"\",");
}else{
sb.append(""+obj[i]+",");
}
}
sb.delete(sb.length()-1, sb.length());
sb.append("]}");
return sb.toString();
} private static Object[] getObjectValue(Object o) throws Exception {
Class<? extends Object> c = o.getClass();
Field[] f = c.getDeclaredFields();
Object[] value = new Object[f.length];
for (int i = 0; i < f.length; i++) {
value[i] = getMethodValue(o, f[i].getName());
}
return value;
}
private static Object getMethodValue(Object owner, String methodName)
throws Exception {
Class<? extends Object> ownerClass = owner.getClass();
methodName = methodName.substring(0, 1).toUpperCase()
+ methodName.substring(1);
Method method = null;
try {
method = ownerClass.getMethod("get" + methodName);
} catch (SecurityException e) {
} catch (NoSuchMethodException e) {
return " can't find 'get" + methodName + "' method";
}
return method.invoke(owner);
}}
我之前用过,用它处里将自符串转对象好像不行,所有API我都看了没有类似的方法.
建议LZ用org.json比net.sf.json好用很多
String str = '';
直接JSONArray arr = new JSONArray(str);
同事好象也都只用org.json和jackson.json
要好用很多
这已经是一个json字符串了呢,楼主的意思是转换成js中的json对象么?有个工具类的https://github.com/douglascrockford/JSON-js