• 高效的JSON处理_ Jackson


    High-performance JSON processor

    高效的JSON处理器


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

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

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

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

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

    转化json字符串:

    /**
    * 使用Jackson生成json格式字符串
    *
    *
    @author archie2010 since 2011-4-26下午05:59:46
    */
    public class JacksonTest {

    private static JsonGenerator jsonGenerator = null;
    private static ObjectMapper objectMapper = null;
    private static User user = null;

    /**
    * 转化实体为json字符串
    *
    @throws IOException
    */
    public static void writeEntity2Json() throws IOException{
    System.out.println(
    "使用JsonGenerator转化实体为json串-------------");
    //writeObject可以转换java对象,eg:JavaBean/Map/List/Array等
    jsonGenerator.writeObject(user);
    System.out.println();
    System.out.println(
    "使用ObjectMapper-----------");
    //writeValue具有和writeObject相同的功能
    objectMapper.writeValue(System.out, user);
    }
    /**
    * 转化Map为json字符串
    *
    @throws JsonGenerationException
    *
    @throws JsonMappingException
    *
    @throws IOException
    */
    public static void writeMap2Json() throws JsonGenerationException, JsonMappingException, IOException{
    System.out.println(
    "转化Map为字符串--------");
    Map
    <String, Object> map=new HashMap<String, Object>();
    map.put(
    "uname", user.getUname());
    map.put(
    "upwd", user.getUpwd());
    map.put(
    "USER", user);
    objectMapper.writeValue(System.out, map);
    }
    /**
    * 转化List为json字符串
    *
    @throws IOException
    *
    @throws JsonMappingException
    *
    @throws JsonGenerationException
    */
    public static void writeList2Json() throws IOException{
    List
    <User> userList=new ArrayList<User>();
    userList.add(user);

    User u
    =new User();
    u.setUid(
    10);
    u.setUname(
    "archie");
    u.setUpwd(
    "123");
    userList.add(u);
    objectMapper.writeValue(System.out, userList);

     

    objectMapper.writeValue(System.out, userList);
    }
    public static void main(String[] args) {
    user
    = new User();
    user.setUid(
    5);
    user.setUname(
    "tom");
    user.setUpwd(
    "123");
    user.setNumber(
    3.44);
    objectMapper
    = new ObjectMapper();
    try {
    jsonGenerator
    = objectMapper.getJsonFactory().createJsonGenerator(System.out, JsonEncoding.UTF8);
    //writeEntity2Json();
    //writeMap2Json();
    writeList2Json();
    }
    catch (IOException e) {

    e.printStackTrace();

    }
    }
    }

    [{"number":3.44,"uname":"tom","upwd":"123","uid":5},{"number":0.0,"uname":"archie","upwd":"123","uid":10}]

    输出到浏览器端:

    StringWriter writer = new StringWriter();
    ObjectMapper mapper
    = new ObjectMapper();
    try {
    mapper.writeValue(writer, hMap);
    }
    catch (JsonGenerationException e) {
    e.printStackTrace();
    }
    catch (JsonMappingException e) {
    e.printStackTrace();
    }
    catch (IOException e) {
    e.printStackTrace();
    }

    response.setContentType(
    "text/html;charset=UTF-8");
    PrintWriter out
    = response.getWriter();

    out.print(writer.toString());


  • 相关阅读:
    存储过程示例
    对话:关于委托的进一步讨论(转)
    Oracle SQLServer 的随机数问题 .(转)
    AJAX控件之AutoComplete
    穷人与富人的区别
    喜欢(转)
    用sql求得每行行号
    ajax 注册
    如何快速生成100万不重复的8位编号 (转)
    存儲過程的基本語句
  • 原文地址:https://www.cnblogs.com/archie2010/p/2032790.html
Copyright © 2020-2023  润新知