java 如何将字符串转json方法

xwt799023 2010-12-07 12:01:07
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
...全文
81962 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
samel_trial 2013-02-28
  • 打赏
  • 举报
回复
org的json的确比nef的好用
wuyuqing1006 2012-03-12
  • 打赏
  • 举报
回复
我也想知道net.sf.json与org.json的区别
laotycoon 2011-07-29
  • 打赏
  • 举报
回复
aaaaa
ajaxuser001 2010-12-15
  • 打赏
  • 举报
回复
手拼json的路过~
mouhk 2010-12-08
  • 打赏
  • 举报
回复
[Quote=引用楼主 xwt799023 的回复:]
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, nam……
[/Quote]

这已经是一个json字符串了呢,楼主的意思是转换成js中的json对象么?有个工具类的https://github.com/douglascrockford/JSON-js
tanwan 2010-12-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 xwt799023 的回复:]
我用的是JQUERY
[/Quote]
你直接eval()下然后直接以节点的形式chirld取就好了么
wingson_shen 2010-12-08
  • 打赏
  • 举报
回复
推荐用GSON,
xwt799023 2010-12-07
  • 打赏
  • 举报
回复
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);
xiaochengfu1 2010-12-07
  • 打赏
  • 举报
回复
下个.jar包就行,如果是Ext的话,就直接用jsonreader吧
wangbayongqq51 2010-12-07
  • 打赏
  • 举报
回复
不懂哈吉
ChDw 2010-12-07
  • 打赏
  • 举报
回复
去下载一个Java版本的JSON就行啦
http://json-lib.sourceforge.net
nanjiwubingqq 2010-12-07
  • 打赏
  • 举报
回复
要去下载一个json的包
  • 打赏
  • 举报
回复
我都是自己用StringBuffered拼的
QQ17348696 2010-12-07
  • 打赏
  • 举报
回复
json的jar包,支持JSON与JAVA 对象互转
huangyunzeng2008 2010-12-07
  • 打赏
  • 举报
回复
应该有相应的解析json的jar包,我对dojo中的比较了解,相信java中肯定有的!
bb12152205gg 2010-12-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 chdw 的回复:]
去下载一个Java版本的JSON就行啦
http://json-lib.sourceforge.net
[/Quote]


学习了
haixiang710 2010-12-07
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 xwt799023 的回复:]
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
[/Quote]
你用的net.sf.json不好用,
我之前用过,用它处里将自符串转对象好像不行,所有API我都看了没有类似的方法.
建议LZ用org.json比net.sf.json好用很多
String str = '';
直接JSONArray arr = new JSONArray(str);
同事好象也都只用org.json和jackson.json
要好用很多
getserved 2010-12-07
  • 打赏
  • 举报
回复
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);
}

}
xwt799023 2010-12-07
  • 打赏
  • 举报
回复
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
haixiang710 2010-12-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xwt799023 的回复:]
JAR包下了
JSONArray json = JSONArray.fromObject(xx);
//JSONArray json = JSONArray.fromObject(modulelist);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncod……
[/Quote]
请问你用的是json的哪个包?json有很多种包形式
是org.json吗?
加载更多回复(5)

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧