- 浏览: 130694 次
- 性别:
- 来自: 深圳
文章分类
最新评论
JSON-lib这个Java类包用于把bean,map和XML转换成JSON并能够把JSON转回成bean和DynaBean。
下载地址:http://json-lib.sourceforge.net/
还要需要的第3方包:
org.apache.commons(3.2以上版本)
org.apache.oro
net.sf.ezmorph(ezmorph-1.0.4.jar)
nu.xom
1、List
1. boolean[] boolArray = new boolean[]{true,false,true};
2. JSONArray jsonArray1 = JSONArray.fromObject( boolArray );
3. System.out.println( jsonArray1 );
4. // prints [true,false,true]
5.
6. List list = new ArrayList();
7. list.add( "first" );
8. list.add( "second" );
9. JSONArray jsonArray2 = JSONArray.fromObject( list );
10. System.out.println( jsonArray2 );
11. / prints ["first","second"]
12.
13. JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" );
14. System.out.println( jsonArray3 );
15. // prints ["json","is","easy"]
16.
17. boolean[] boolArray = new boolean[]{true,false,true};
18. JSONArray jsonArray1 = JSONArray.fromObject( boolArray );
19. System.out.println( jsonArray1 );
20. // prints [true,false,true]
21.
22. List list = new ArrayList();
23. list.add( "first" );
24. list.add( "second" );
25. JSONArray jsonArray2 = JSONArray.fromObject( list );
26. System.out.println( jsonArray2 );
27. // prints ["first","second"]
28.
29. JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" );
30. System.out.println( jsonArray3 );
31. // prints ["json","is","easy"]
2、Map
1. Map map = new HashMap();
2. map.put( "name", "json" );
3. map.put( "bool", Boolean.TRUE );
4.
5. map.put( "int", new Integer(1) );
6. map.put( "arr", new String[]{"a","b"} );
7. map.put( "func", "function(i){ return this.arr[i]; }" );
8. JSONObject json = JSONObject.fromObject( map );
9. System.out.println( json );
10. //{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true}
11.
12. Map map = new HashMap();
13. map.put( "name", "json" );
14. map.put( "bool", Boolean.TRUE );
15.
16. map.put( "int", new Integer(1) );
17. map.put( "arr", new String[]{"a","b"} );
18. map.put( "func", "function(i){ return this.arr[i]; }" );
19. JSONObject json = JSONObject.fromObject( map );
20. System.out.println( json );
21. //{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true}
3、BEAN
1.
2. JSONObject jsonObject = JSONObject.fromObject( new JsonBean() );
3. System.out.println( jsonObject );
4. //{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}
5.
6.
7. JSONObject jsonObject = JSONObject.fromObject( new JsonBean() );
8. System.out.println( jsonObject );
9. //{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}
10.
4、BEANS
1.
2. List list = new ArrayList();
3. JsonBean2 jb1 = new JsonBean2();
4. jb1.setCol(1);
5. jb1.setRow(1);
6. jb1.setValue("xx");
7.
8. JsonBean2 jb2 = new JsonBean2();
9. jb2.setCol(2);
10. jb2.setRow(2);
11. jb2.setValue("");
12.
13. list.add(jb1);
14. list.add(jb2);
15.
16. JSONArray ja = JSONArray.fromObject(list);
17. System.out.println( ja.toString() );
18. //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]
19.
20.
21. List list = new ArrayList();
22. JsonBean2 jb1 = new JsonBean2();
23. jb1.setCol(1);
24. jb1.setRow(1);
25. jb1.setValue("xx");
26.
27. JsonBean2 jb2 = new JsonBean2();
28. jb2.setCol(2);
29. jb2.setRow(2);
30. jb2.setValue("");
31.
32.
33. list.add(jb1);
34. list.add(jb2);
35.
36. JSONArray ja = JSONArray.fromObject(list);
37. System.out.println( ja.toString() );
38. //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]
5、String to bean
1. String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
2. JSONObject jsonObject = JSONObject.fromString(json);
3. Object bean = JSONObject.toBean( jsonObject );
4. assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );
5. assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );
6. assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );
7. assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );
8. assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );
9. List expected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );
10. assertEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) );
11.
12. String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
13. JSONObject jsonObject = JSONObject.fromString(json);
14. Object bean = JSONObject.toBean( jsonObject );
15. assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );
16. assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );
17. assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );
18. assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );
19. assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );
20. List expected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );
21. assertEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) );
22.
1. String json = "{\"value\":\"xx\",\"row\":1,\"col\":1}";
2. JSONObject jsonObject = JSONObject.fromString(json);
3. JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class );
4. assertEquals( jsonObject.get( "col" ),new Integer( bean.getCol()) );
5. assertEquals( jsonObject.get( "row" ), new Integer( bean.getRow() ) );
6. assertEquals( jsonObject.get( "value" ), bean.getValue() );
7.
8. String json = "{\"value\":\"xx\",\"row\":1,\"col\":1}";
9. JSONObject jsonObject = JSONObject.fromString(json);
10. JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class );
11. assertEquals( jsonObject.get( "col" ),new Integer( bean.getCol()) );
12. assertEquals( jsonObject.get( "row" ), new Integer( bean.getRow() ) );
13. assertEquals( jsonObject.get( "value" ), bean.getValue() );
6 json to xml
1)
1. JSONObject json = new JSONObject( true );
2. String xml = XMLSerializer.write( json );
1. < o class="object" null="true">
2)
1. JSONObject json = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}");
2. String xml = XMLSerializer.write( json );
1. < o class="object">
2. < name type="string">json< /name>
3. < bool type="boolean">true< /bool>
4. < int type="number">1< /int>
5. < /o>
6. < o class="object">
7. < name type="string">json< /name>
8. < bool type="boolean">true< /bool>
9. < int type="number">1< /int>
10. < /o>
3)
1. JSONArray json = JSONArray.fromObject("[1,2,3]");
2. String xml = XMLSerializer.write( json );
1. < a class="array">
2. < e type="number">1< /e>
3. < e type="number">2< /e>
4. < e type="number">3< /e>
5. < /a>
7 、xml to json
1. < a class="array">
2. < e type="function" params="i,j">
3. return matrix[i][j];
4. < /e>
5. < /a>
6. < a class="array">
7. < e type="function" params="i,j">
8. return matrix[i][j];
9. < /e>
10. < /a>
1. JSONArray json = (JSONArray) XMLSerializer.read( xml );
2. System.out.println( json );
3. // prints [function(i,j){ return matrix[i][j]; }]
下载地址:http://json-lib.sourceforge.net/
还要需要的第3方包:
org.apache.commons(3.2以上版本)
org.apache.oro
net.sf.ezmorph(ezmorph-1.0.4.jar)
nu.xom
1、List
1. boolean[] boolArray = new boolean[]{true,false,true};
2. JSONArray jsonArray1 = JSONArray.fromObject( boolArray );
3. System.out.println( jsonArray1 );
4. // prints [true,false,true]
5.
6. List list = new ArrayList();
7. list.add( "first" );
8. list.add( "second" );
9. JSONArray jsonArray2 = JSONArray.fromObject( list );
10. System.out.println( jsonArray2 );
11. / prints ["first","second"]
12.
13. JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" );
14. System.out.println( jsonArray3 );
15. // prints ["json","is","easy"]
16.
17. boolean[] boolArray = new boolean[]{true,false,true};
18. JSONArray jsonArray1 = JSONArray.fromObject( boolArray );
19. System.out.println( jsonArray1 );
20. // prints [true,false,true]
21.
22. List list = new ArrayList();
23. list.add( "first" );
24. list.add( "second" );
25. JSONArray jsonArray2 = JSONArray.fromObject( list );
26. System.out.println( jsonArray2 );
27. // prints ["first","second"]
28.
29. JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" );
30. System.out.println( jsonArray3 );
31. // prints ["json","is","easy"]
2、Map
1. Map map = new HashMap();
2. map.put( "name", "json" );
3. map.put( "bool", Boolean.TRUE );
4.
5. map.put( "int", new Integer(1) );
6. map.put( "arr", new String[]{"a","b"} );
7. map.put( "func", "function(i){ return this.arr[i]; }" );
8. JSONObject json = JSONObject.fromObject( map );
9. System.out.println( json );
10. //{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true}
11.
12. Map map = new HashMap();
13. map.put( "name", "json" );
14. map.put( "bool", Boolean.TRUE );
15.
16. map.put( "int", new Integer(1) );
17. map.put( "arr", new String[]{"a","b"} );
18. map.put( "func", "function(i){ return this.arr[i]; }" );
19. JSONObject json = JSONObject.fromObject( map );
20. System.out.println( json );
21. //{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true}
3、BEAN
1.
2. JSONObject jsonObject = JSONObject.fromObject( new JsonBean() );
3. System.out.println( jsonObject );
4. //{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}
5.
6.
7. JSONObject jsonObject = JSONObject.fromObject( new JsonBean() );
8. System.out.println( jsonObject );
9. //{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}
10.
4、BEANS
1.
2. List list = new ArrayList();
3. JsonBean2 jb1 = new JsonBean2();
4. jb1.setCol(1);
5. jb1.setRow(1);
6. jb1.setValue("xx");
7.
8. JsonBean2 jb2 = new JsonBean2();
9. jb2.setCol(2);
10. jb2.setRow(2);
11. jb2.setValue("");
12.
13. list.add(jb1);
14. list.add(jb2);
15.
16. JSONArray ja = JSONArray.fromObject(list);
17. System.out.println( ja.toString() );
18. //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]
19.
20.
21. List list = new ArrayList();
22. JsonBean2 jb1 = new JsonBean2();
23. jb1.setCol(1);
24. jb1.setRow(1);
25. jb1.setValue("xx");
26.
27. JsonBean2 jb2 = new JsonBean2();
28. jb2.setCol(2);
29. jb2.setRow(2);
30. jb2.setValue("");
31.
32.
33. list.add(jb1);
34. list.add(jb2);
35.
36. JSONArray ja = JSONArray.fromObject(list);
37. System.out.println( ja.toString() );
38. //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]
5、String to bean
1. String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
2. JSONObject jsonObject = JSONObject.fromString(json);
3. Object bean = JSONObject.toBean( jsonObject );
4. assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );
5. assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );
6. assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );
7. assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );
8. assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );
9. List expected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );
10. assertEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) );
11.
12. String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
13. JSONObject jsonObject = JSONObject.fromString(json);
14. Object bean = JSONObject.toBean( jsonObject );
15. assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );
16. assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );
17. assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );
18. assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );
19. assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );
20. List expected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );
21. assertEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) );
22.
1. String json = "{\"value\":\"xx\",\"row\":1,\"col\":1}";
2. JSONObject jsonObject = JSONObject.fromString(json);
3. JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class );
4. assertEquals( jsonObject.get( "col" ),new Integer( bean.getCol()) );
5. assertEquals( jsonObject.get( "row" ), new Integer( bean.getRow() ) );
6. assertEquals( jsonObject.get( "value" ), bean.getValue() );
7.
8. String json = "{\"value\":\"xx\",\"row\":1,\"col\":1}";
9. JSONObject jsonObject = JSONObject.fromString(json);
10. JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class );
11. assertEquals( jsonObject.get( "col" ),new Integer( bean.getCol()) );
12. assertEquals( jsonObject.get( "row" ), new Integer( bean.getRow() ) );
13. assertEquals( jsonObject.get( "value" ), bean.getValue() );
6 json to xml
1)
1. JSONObject json = new JSONObject( true );
2. String xml = XMLSerializer.write( json );
1. < o class="object" null="true">
2)
1. JSONObject json = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}");
2. String xml = XMLSerializer.write( json );
1. < o class="object">
2. < name type="string">json< /name>
3. < bool type="boolean">true< /bool>
4. < int type="number">1< /int>
5. < /o>
6. < o class="object">
7. < name type="string">json< /name>
8. < bool type="boolean">true< /bool>
9. < int type="number">1< /int>
10. < /o>
3)
1. JSONArray json = JSONArray.fromObject("[1,2,3]");
2. String xml = XMLSerializer.write( json );
1. < a class="array">
2. < e type="number">1< /e>
3. < e type="number">2< /e>
4. < e type="number">3< /e>
5. < /a>
7 、xml to json
1. < a class="array">
2. < e type="function" params="i,j">
3. return matrix[i][j];
4. < /e>
5. < /a>
6. < a class="array">
7. < e type="function" params="i,j">
8. return matrix[i][j];
9. < /e>
10. < /a>
1. JSONArray json = (JSONArray) XMLSerializer.read( xml );
2. System.out.println( json );
3. // prints [function(i,j){ return matrix[i][j]; }]
发表评论
-
note
2013-11-20 15:38 1014groupadd hadoop useradd hadoo ... -
阻塞队列
2013-10-17 10:15 760import java.io.*; impor ... -
生产者和消费者
2013-10-14 15:58 1import java.util.Stack; ... -
排序代码
2013-10-08 14:17 794import java.util.Arrays; ... -
Spring中的事务传播属性详解
2013-06-07 17:10 973文章不错,转载于:htt ... -
java中byte和int的那些事
2013-05-30 17:50 681java基本数据类型转换,看似简单,可很多人在工作编程中却 ... -
单例?
2013-05-24 22:15 761老是有人喜欢装X的问单例,话说下面是一种最靠谱的答案 ... -
cfx 调用webservice方式
2013-05-24 15:30 6681方式一:通过ClientProxyFactoryBean 调 ... -
Tomcat 配置访问权限
2013-04-08 12:03 1468tomcat貌似有个比较好玩的东西,有时候我们的应用 ... -
ibatis 修改sql文件不用重启服务器
2013-04-07 14:45 0ibatis在开发的时候每次修改都需要重启应用服务器,所以 ... -
apache cxf Client (soupUI)
2013-03-28 11:56 1146public class CxfClientT ... -
xfire
2013-03-27 12:40 0applicationContext.xml ... -
apche cxf
2013-03-27 11:52 0web.xml <servlet> ... -
structs2 漏洞代码
2013-02-01 17:52 0structs2 漏洞 ?('\u0023_ ... -
netty版telnet Server端
2013-02-01 09:13 0package netty; import ja ... -
spring mvc 2.5
2013-01-09 11:05 1229spring mvc使用笔记(基于xml配置文件) 需要三个 ... -
递归求组合?
2012-12-14 14:38 745比如【1,2,3】 获得其全部的排列组合,包括1个数字的 ... -
小试JFreeChart柱状图和饼状图
2012-07-11 20:44 2654闲来无事,就学学报表技术,目前找到一款开源JFreeC ... -
netty小试Telnet
2012-07-07 15:57 4053使用netty小时telnet一把,引入netty ... -
java操作ActiveMQ
2012-07-07 13:47 1076package mq; import javax.jm ...
相关推荐
Java JSON API是Java平台上的库,提供了处理JSON的能力,包括解析JSON字符串、生成JSON对象以及进行JSON与Java对象之间的转换。 在Java中,有多种实现JSON API的库,如Jackson、Gson、org.json和json-lib等。本篇...
Java JSON 依赖包是开发Java应用时处理JSON数据的关键组件,尤其在进行Web服务或前后端交互时。JSON,全称JavaScript Object Notation,是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。...
1. `json-lib-2.3-jdk15.jar`:这可能是JSON库的一个版本,JSON Lib是一个Java实现的JSON处理器,它提供了将Java对象转换为JSON格式和从JSON反序列化回Java对象的功能。版本2.3-jdk15可能表示它兼容Java 1.5版本。 ...
Java JSON的库主要被用来处理JSON(JavaScript Object Notation)数据格式,这是一种轻量级的数据交换格式,广泛用于客户端和服务器之间的数据传输。在Java中,有多种JSON库可供选择,如Jackson、Gson、org.json等。...
Java JSON技术是开发中常用的序列化和反序列化工具,特别是在Web服务和后端开发中。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。在Java中,处理...
JSON-B(Java API for JSON Binding)是Java的一个标准,类似于JAXB,但用于JSON和Java对象之间的自动绑定。它简化了JSON与Java对象的转换过程。 这些jar包包含了处理JSON所需的各种工具和功能,例如: - JSON解析...
Java JSON Jar包是Java开发中处理JSON数据必不可少的工具库,尤其在进行Web服务或前后端交互时。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,它以其简洁和高效而受到广泛欢迎。在Java中,如果...
Java中的json-lib库是处理JSON数据的一个常用工具,它提供了将Java对象转换为JSON字符串以及从JSON字符串反序列化回Java对象的功能。然而,在处理复杂的嵌套数据结构时,如果不小心可能会导致无限循环的问题。 在...
该压缩包包含json使用到的所有jar,jar版本如下: commons-beanutils-1.9.2.jar,commons-collections-3.2.1.jar,commons-lang-2.6.jar, commons-logging-1.1.1.jar,ezmorph-1.0.6.jar,json-lib-2.4-jdk15.jar ...
JsonPath 是java Json库,最新源代码JsonPath 是java Json库,最新源代码JsonPath 是java Json库,最新源代码JsonPath 是java Json库,最新源代码JsonPath 是java Json库,最新源代码JsonPath 是java Json库,最新源...
Java中的`javajson`包,就是为了让Java开发者能够方便地处理JSON数据,将Java对象转换为JSON字符串,或者将JSON字符串解析为Java对象。 在给定的压缩包文件中,我们看到几个关键的库文件,这些是`javajson`包正常...
里面包含所有java 使用json所需要的jar包,包含json-lib-2.4-jdk15,ezmorph-1.0.6,commons-logging-1.1.1,commons-lang-2.5,commons-beanutils-1.8.0,commons-collections-3.2.1
关于java json解析的简单测试,简单示例
Java中的JSON(JavaScript Object Notation)序列化与反序列化是开发过程中常见且重要的操作,主要用于数据交换和存储。JSON是一种轻量级的数据交换格式,它允许我们将Java对象转换为JSON字符串,反之亦然,方便在...
1. **json-lib**: `json-lib-2.2.2-jdk15.jar`是这个集合中的核心包,它是一个Java实现的JSON库,提供了将Java对象转换为JSON格式和从JSON字符串还原Java对象的功能。支持多种Java类型,如JavaBean、Map、List等,...
java json所依赖的所有jar包(json.jar.rar) java json所依赖的所有jar包(json.jar.rar) java json所依赖的所有jar包(json.jar.rar)
本文将详细探讨Java中JSON与XML的互转,以及如何在实际开发中运用这些技术。 标题:“java json xml转换” 描述:“java版的json与xml之间的转换的实例demo” JSON和XML各有优缺点。JSON以其简洁、易于阅读和生成...
java json 工具类java json 工具类 java json 工具类java json 工具类 java json 工具类java json 工具类 java json 工具类java json 工具类
`javajson.zip`这个压缩包可能包含的是与Java处理JSON相关的类库或者示例代码。虽然没有具体文件内容,但我们可以根据常见场景来分析其中可能涵盖的知识点。 1. **JSON基本结构**:JSON由对象和数组构成,对象是...
1. Jackson:Jackson是Java中广泛使用的JSON库,提供了高性能的读写功能,支持多种API,如`ObjectMapper`用于对象与JSON之间的转换,`JsonNode`用于处理JSON树结构。 2. Gson:Google提供的Gson库可以轻松地将Java...