首页 > 代码库 > Jackson.jar的使用记录
Jackson.jar的使用记录
Jackson.jar的使用记录
之前一直使用json-lib.jar,最近发现网上说这个jackson.jar比较好
package com.spring.controller; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.codehaus.jackson.JsonEncoding; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectWriter; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller public class JacksonController { @RequestMapping(value=http://www.mamicode.com/"user/jackson", method = {RequestMethod.POST,RequestMethod.GET})>list转为json数组(是Java main程序)
package com.main.java.demo; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectWriter; public class JacksonDemo { public static void main(String args[]) throws IOException{ //writeListToJsonArray(); //writeListToJsonArray2(); //writeListToJsonArray3(); //writeListToJsonArray4(); //writeListToJsonArray5(); writeListToJsonArray6(); } /** * list转为json数组 */ public static String writeListToJsonArray() throws IOException { /*List<Event> list = new ArrayList<Event>(2); list.add(new Event("a1","a2")); list.add(new Event("b1","b2"));*/ /*List<String> list = new ArrayList<String>(); list.add("A2"); list.add("B2");*/ /*List<Integer> list = new ArrayList<Integer>(); list.add(12); list.add(45); list.add(5);*/ List<Float> list = new ArrayList<Float>(); list.add((float) 12.32); list.add((float) 45.12); list.add((float) 5.09); OutputStream out = new ByteArrayOutputStream(); ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(out, list); byte[] data = http://www.mamicode.com/((ByteArrayOutputStream) out).toByteArray();>
jsp页面:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href=http://www.mamicode.com/"">>Jackson.jar的使用记录
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。