免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2570 | 回复: 0
打印 上一主题 下一主题

高效的JSON处理器 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-04-29 17:26 |只看该作者 |倒序浏览
转:    archie2010

高效的JSON处理器



JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。

易于人阅读和编写。同时也易于机器解析和生成

JSON-lib官网:http://json-lib.sourceforge.net/

Jackson官网:http://jackson.codehaus.org/

号称性能最快的JSON处理器Jackson远高于JSON_lib

转化json字符串:

  1. /** * 使用Jackson生成json格式字符串 *
  2.   * @author archie2010 since 2011-4-26下午05:59:46
  3. */public class JacksonTest {
  4.     private static JsonGenerator jsonGenerator = null;
  5.     private static ObjectMapper objectMapper = null;
  6.     private static User user = null;
  7.     /**
  8.      * 转化实体为json字符串
  9.      * @throws IOException
  10.      */    public static void writeEntity2Json() throws IOException{
  11.         System.out.println("使用JsonGenerator转化实体为json串-------------");
  12.         //writeObject可以转换java对象,eg:JavaBean/Map/List/Array等
  13.         jsonGenerator.writeObject(user);
  14.         System.out.println();
  15.         System.out.println("使用ObjectMapper-----------");
  16.         //writeValue具有和writeObject相同的功能
  17.         objectMapper.writeValue(System.out, user);
  18.     }
  19.     /**
  20.      * 转化Map为json字符串
  21.      * @throws JsonGenerationException
  22.      * @throws JsonMappingException
  23.      * @throws IOException
  24.      */    public static void writeMap2Json() throws JsonGenerationException, JsonMappingException, IOException{
  25.         System.out.println("转化Map为字符串--------");
  26.         Map<String, Object> map=new HashMap<String, Object>();
  27.         map.put("uname", user.getUname());
  28.         map.put("upwd", user.getUpwd());
  29.         map.put("USER", user);
  30.         objectMapper.writeValue(System.out, map);
  31.     }
  32.     /**
  33.      * 转化List为json字符串
  34.      * @throws IOException
  35.      * @throws JsonMappingException
  36.       * @throws JsonGenerationException
  37.       */    public static void writeList2Json() throws IOException{
  38.         List<User> userList=new ArrayList<User>();
  39.         userList.add(user);
  40.                 User u=new User();
  41.         u.setUid(10);
  42.         u.setUname("archie");
  43.         u.setUpwd("123");
  44.         userList.add(u);
  45.         objectMapper.writeValue(System.out, userList);
  46.                                  objectMapper.writeValue(System.out, userList);
  47.     }
  48.     public static void main(String[] args) {
  49.         user = new User();
  50.         user.setUid(5);
  51.         user.setUname("tom");
  52.         user.setUpwd("123");
  53.         user.setNumber(3.44);
  54.         objectMapper = new ObjectMapper();
  55.         try {
  56.             jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(System.out, JsonEncoding.UTF8);
  57.             //writeEntity2Json();
  58.             //writeMap2Json();
  59.             writeList2Json();
  60.         } catch (IOException e) {
  61.             e.printStackTrace();
  62.         }
  63.     }}[{"number":3.44,"uname":"tom","upwd":"123","uid":5},{"number":0.0,"uname":"archie","upwd":"123","uid":10}]
复制代码
输出到浏览器端:

  1. StringWriter writer = new StringWriter();
  2. ObjectMapper mapper = new ObjectMapper();
  3.   try {
  4.    mapper.writeValue(writer, hMap);
  5.   } catch (JsonGenerationException e) {
  6.    e.printStackTrace();
  7.   } catch (JsonMappingException e) {
  8.    e.printStackTrace();
  9.   } catch (IOException e) {
  10.    e.printStackTrace();
  11.   }
  12. response.setContentType("text/html;charset=UTF-8");
  13. PrintWriter out= response.getWriter();out.print(writer.toString());
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP