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

解决方案 »

  1.   

    去下载一个Java版本的JSON就行啦
    http://json-lib.sourceforge.net
      

  2.   

    下个.jar包就行,如果是Ext的话,就直接用jsonreader吧
      

  3.   

    JAR包下了
    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);
      

  4.   

    下载jar包,然后
    JSONArray json = JSONArray.fromObject(xx);
    然后,转成单个对象的话
    遍历JSONArray
    会用到:JSONObject job = json.getBean();
      

  5.   

    可不可以一下子都转过来,然后通过
    HttpServletResponse response = ServletActionContext.getResponse();
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/plain");   
    response.getWriter().println(json );  
    传到前台去,那位知道?
      

  6.   

    请问你用的是json的哪个包?json有很多种包形式
    是org.json吗?
      

  7.   

    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
      

  8.   

    package struts.controller.tool;import java.lang.reflect.Field;
    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);
    }}
      

  9.   

    你用的net.sf.json不好用,
    我之前用过,用它处里将自符串转对象好像不行,所有API我都看了没有类似的方法.
    建议LZ用org.json比net.sf.json好用很多
    String str = '';
    直接JSONArray arr = new JSONArray(str);
    同事好象也都只用org.json和jackson.json
    要好用很多
      

  10.   

    应该有相应的解析json的jar包,我对dojo中的比较了解,相信java中肯定有的!
      

  11.   

    json的jar包,支持JSON与JAVA 对象互转
      

  12.   

    我都是自己用StringBuffered拼的
      

  13.   

    你直接eval()下然后直接以节点的形式chirld取就好了么
      

  14.   


    这已经是一个json字符串了呢,楼主的意思是转换成js中的json对象么?有个工具类的https://github.com/douglascrockford/JSON-js
      

  15.   

    我也想知道net.sf.json与org.json的区别