java 如何将字符串转json方法

时间:2023-02-12 19:35:28
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

28 个解决方案

#1


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

#2


不懂哈吉

#3


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

#4


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);

#5


引用楼主 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……

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

#6


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

#7


我用的是JQUERY

#8


哈哈。。。。。

#9


那位知道?

#10


引用 4 楼 xwt799023 的回复:
JAR包下了
JSONArray json = JSONArray.fromObject(xx);
//JSONArray json = JSONArray.fromObject(modulelist);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncod……

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

#11


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

#12


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);
}

}

#13


该回复于2010-12-07 14:24:48被版主删除

#14


引用 11 楼 xwt799023 的回复:
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

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

#15


该回复于2010-12-08 10:09:02被版主删除

#16


该回复于2010-12-08 08:57:00被版主删除

#17


引用 1 楼 chdw 的回复:
去下载一个Java版本的JSON就行啦
http://json-lib.sourceforge.net



学习了

#18


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

#19


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

#20


我都是自己用StringBuffered拼的

#21


要去下载一个json的包

#22


推荐用GSON,

#23


引用 7 楼 xwt799023 的回复:
我用的是JQUERY

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

#24


引用楼主 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……


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

#25


手拼json的路过~

#26


aaaaa

#27


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

#28


org的json的确比nef的好用

#1


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

#2


不懂哈吉

#3


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

#4


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);

#5


引用楼主 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……

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

#6


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

#7


我用的是JQUERY

#8


哈哈。。。。。

#9


那位知道?

#10


引用 4 楼 xwt799023 的回复:
JAR包下了
JSONArray json = JSONArray.fromObject(xx);
//JSONArray json = JSONArray.fromObject(modulelist);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncod……

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

#11


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

#12


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);
}

}

#13


该回复于2010-12-07 14:24:48被版主删除

#14


引用 11 楼 xwt799023 的回复:
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

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

#15


该回复于2010-12-08 10:09:02被版主删除

#16


该回复于2010-12-08 08:57:00被版主删除

#17


引用 1 楼 chdw 的回复:
去下载一个Java版本的JSON就行啦
http://json-lib.sourceforge.net



学习了

#18


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

#19


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

#20


我都是自己用StringBuffered拼的

#21


要去下载一个json的包

#22


推荐用GSON,

#23


引用 7 楼 xwt799023 的回复:
我用的是JQUERY

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

#24


引用楼主 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……


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

#25


手拼json的路过~

#26


aaaaa

#27


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

#28


org的json的确比nef的好用