json-lib的依赖包 :
commons-logging.jar
common.lang.jar
xom.jar(XMLObject Model)
commons-beanutils.jar
commons-collections-3.0.jar
ezmorph-0.9.2.jar
大概就是这些,第一个例子搞了一一个小时,依赖包太多了
json-lib可以将java中的Array,Collections,Object,XML转化成json对象
http://json-lib.sourceforge.net/usage.html
我按照官方文档写的例子:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.*;
public class Test {
/**
* @param args
*/
private static boolean[] boolArray={true,false};
private static List list = new ArrayList();
private static Map map=new HashMap();
private static Object[] test={new TestBean(),new TestBean()};
public static void main(String[] args) {
//转换Array
JSONArray jsonArray = JSONArray.fromObject( boolArray );
//System.out.println(jsonArray.toString());
JSONArray jsonArray1=JSONArray.fromObject("['1','2','3']");
//System.out.println(jsonArray1.toString());
list.add("first");
list.add("second");
//list.add(new TestBean());
//转换List
JSONArray jsonList = JSONArray.fromObject(list);
System.out.println(jsonList);
//转换map
map.put("name", "windfree");
map.put("bool", Boolean.TRUE);
map.put("int",new Integer(0));
map.put( "arr", new String[]{"a","b"} );
map.put("func", "function (i){ return this.arr[i]; }");
map.put("bean",test);
JSONObject jsonMap = JSONObject.fromObject(map);
//System.out.println(jsonMap);
//转换Bean
JSONObject jsonBean=JSONObject.fromObject(new TestBean());
//System.out.println(jsonBean);
}
}输出结果
: [true,false]
["1","2","3"]
["first","second"]
{"bean":[{"name":"windfree","pojoId":1,"func1":function(i){ return this.options[i]; },"func2":function(i){ return this.options[i]; },"options":["a","f"]},
{"name":"windfree","pojoId":1,
"func1":function(i){ return this.options[i]; },
"func2":function(i){ return this.options[i]; },
"options":["a","f"]}],"arr":["a","b"],"int":0,"name":"windfree",
"func":function (i){ return this.arr[i]; },
"bool":true}
{"name":"windfree","pojoId":1,"func1":function(i){ return this.options[i]; },"func2":function(i){ return this.options[i]; },"options":["a","f"]}最近也在看GWT,GWT包中有一关于json的例子,
http://code.google.com/webtoolkit/documentation/examples/jsonrpc/demo.html 例子中的json对象为一个js文件,我用servlet重新写了一下。
Thumbnail类:
package com.windfree.server;
public class Thumbnail {
private String Url="http://scd.mm-c1.yimg.com/image/1135881977";
private int Height=120;
private int Width=99;
public int getHeight() {
return Height;
}
public void setHeight(int height) {
Height = height;
}
public String getUrl() {
return Url;
}
public void setUrl(String url) {
Url = url;
}
public int getWidth() {
return Width;
}
public void setWidth(int width) {
Width = width;
}
public Thumbnail(String url, int height, int width) {
super();
Url = url;
Height = height;
Width = width;
}
}Result类:
package com.windfree.server;
public class Result {
private String Title;
private String Url;
private String ClickUrl;
private String RefererUrl;
private String FileFormat;
private int Height;
private int Width;
private Thumbnail thumbnail;
public Result(String title, String url, String clickUrl, String refererUrl, String fileFormat, int height, int width, Thumbnail thumbnail) {
super();
Title = title;
Url = url;
ClickUrl = clickUrl;
RefererUrl = refererUrl;
FileFormat = fileFormat;
Height = height;
Width = width;
this.thumbnail = thumbnail;
}
public String getClickUrl() {
return ClickUrl;
}
public void setClickUrl(String clickUrl) {
ClickUrl = clickUrl;
}
public String getFileFormat() {
return FileFormat;
}
public void setFileFormat(String fileFormat) {
FileFormat = fileFormat;
}
public int getHeight() {
return Height;
}
public void setHeight(int height) {
Height = height;
}
public String getRefererUrl() {
return RefererUrl;
}
public void setRefererUrl(String refererUrl) {
RefererUrl = refererUrl;
}
public Thumbnail getThumbnail() {
return thumbnail;
}
public void setThumbnail(Thumbnail thumbnail) {
this.thumbnail = thumbnail;
}
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public String getUrl() {
return Url;
}
public void setUrl(String url) {
Url = url;
}
public int getWidth() {
return Width;
}
public void setWidth(int width) {
Width = width;
}
}JSONHttpServlet类:
package com.windfree.server;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
public class JSONHttpServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private static Map map=new HashMap();
private static Map map2=new HashMap();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Thumbnail tmail1=new Thumbnail("http://scd.mm-c1.yimg.com/image/1135881977",200,203);
Thumbnail tmail2=new Thumbnail("http://scd.mm-c1.yimg.com/image/1135881977",100,100);
Result reulst1=new Result("111","222","333","444","555",100,100,tmail1);
Result reulst2=new Result("111","222","333","444","555",100,100,tmail2);
map.put("totalResultsAvailable", "144429");
map.put("totalResultsReturned", 2);
map.put("firstResultPosition", 1);
map.put("result",new Result[]{reulst1,reulst2});
map2.put("Results", map);
JSONObject jsonMap=JSONObject.fromObject(map2);
PrintWriter out = response.getWriter();
out.println(jsonMap.toString());
}
}在例子中的配置文件中的配置文件(JSON.gwt.xml)增加
<servlet path="/jsonTest"
class="com.windfree.server.JSONHttpServlet"/>
同时将JSON.java中的URL改为/jsonTest。就可以了。
结果:
java类转为json字符串自定义格式 java class转json
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
java json字符串转 jsonobject
java json字符串转 jsonobject
JSON json 字符串转换 -
Java设置JSON字符串参数编码
本文详细介绍了如何在Java中创建JSON字符串以及在Java中设置JSON字符串参数编码的方法。
json 字符串 JSON Java -
json字符串转java对象 json字符串转为对象
在数据传输过程中,json是以文本,即字符串的形式传递的,而js操作的是JSON对象,所以JSON对象和JSON字符串之间的相互转换是关键
json字符串转java对象 JSON 字符串 解析器