Chinaunix

标题: 高效的JSON处理器 [打印本页]

作者: feiyang10086    时间: 2011-04-29 17:26
标题: 高效的JSON处理器
转:    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());
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2