体验Jackson Json

本文介绍了如何使用 Jackson 库进行 Java 对象与 JSON 字符串之间的转换。包括 List、Map 和 POJO 类型的数据结构,并提供了详细的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

体验了一把Jackson Json。

将对象(List, Map, POJO Object)转换为json字符串: 

可使用方法 ObjectMapper#writeValueAsString(Object value)

例:

list2Json();

map2Json();

pojo2Json();

将Json字符串转换为对象(List, Map, POJO Object): 

可使用方法 ObjectMapper#readValue(String content, Class<T> valueType)

例:

json2List();

json2Map();

json2Pojo();


测试程序如下:

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;


public class ConvertTest {

	public static void main(String[] args) {
		new ConvertTest().todo();
	}
	
	private ObjectMapper mapper = new ObjectMapper();
	
	public void todo() {
		System.out.println("============ Object To JSON ============");
		list2Json();
		map2Json();
		pojo2Json();

		System.out.println("============ JSON TO Object ============");
		json2List();
		json2Map();
		json2Pojo();
	}
	
	public void list2Json() {
			List list = new ArrayList();
			Map m1 = new HashMap();
			m1.put("first", "Joe");
			m1.put("last", "Sixpack");
			m1.put("gender", "MALE");
			m1.put("verified", false);
			list.add(m1);
			Map m2 = new HashMap();
			m2.put("first", "Joe");
			m2.put("last", "Sixpack");
			m2.put("address", "unknown");
			list.add(m2);
			
			String output;
			try {
				output = mapper.writeValueAsString(list);
				System.out.println(output);
				// print '[{"last":"Sixpack","verified":false,"gender":"MALE","first":"Joe"},{"last":"Sixpack","address":"unknown","first":"Joe"}]'
			} catch (JsonGenerationException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	
	public void map2Json() {
		Map m = new HashMap();
		Map m1 = new HashMap();
		m1.put("first", "Joe");
		m1.put("last", "Sixpack");
		m.put("name", m1);
		m.put("gender", "MALE");
		m.put("verified", false);
		
		String output;
		try {
			output = mapper.writeValueAsString(m);
			System.out.println(output); // print '{"verified":false,"name":{"last":"Sixpack","first":"Joe"},"gender":"MALE"}'
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public void pojo2Json() {
		User user = new User();
		User.Name name = new User.Name();
		name.setFirst("Joe");
		name.setLast("Sixpack");
		user.setName(name);
		user.setVerified(false);
		
		String output;
		try {
			output = mapper.writeValueAsString(user);
			System.out.println(output); // print '{"verified":false,"name":{"first":"Joe","last":"Sixpack"}}'
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public void json2List() {
		String json = 
			"[" +
				"{" + 
					"\"name\" : { \"first\" : \"Joe\", \"last\" : \"Sixpack\" }, " +  
					"\"gender\" : \"MALE\", " +
					"\"verified\" : false" + 
				"}" + 
				", " + 
				"{" + 
					"\"name\" : { \"first\" : \"Joe\", \"last\" : \"Sixpack\" }, " +  
					"\"address\" : \"unknown\" " + 
				"}" + 
			"]";
			try {
				List li = mapper.readValue(json, List.class);
				System.out.println(li); // print '[{name={first=Joe, last=Sixpack}, gender=MALE, verified=false}, {name={first=Joe, last=Sixpack}, address=unknown}]'
				String address = (String)((Map)li.get(1)).get("address");
				System.out.println("list(0) => address : " + address); // print 'list(0) => address : unknown'
			} catch (JsonParseException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	
	public void json2Map() {
		String json = 
		"{" + 
			"\"name\" : { \"first\" : \"Joe\", \"last\" : \"Sixpack\" }, " +  
			"\"gender\" : \"MALE\", " +
			"\"verified\" : false" + 
		"}";
		try {
			Map m = mapper.readValue(json, Map.class);
			Object o = ((Map)m.get("name")).get("first");
			System.out.println((String)o); // print 'Joe'
			System.out.println(m); // print '{name={first=Joe, last=Sixpack}, gender=MALE, verified=false}'
		
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void json2Pojo() {
		String json = 
			"{" + 
				"\"name\" : { \"first\" : \"Joe\", \"last\" : \"Sixpack\" }, " +  
				"\"verified\" : false" + 
			"}";
		try {
			User u = mapper.readValue(json, User.class);
			// print 
			// name=>Joe Sixpack
			// verified=>false
			System.out.println(u);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static class User {
		public static class Name {
			  private String _first, _last;
			  public String getFirst() { return _first; }
			  public String getLast() { return _last; }
			  public void setFirst(String s) { _first = s; }
			  public void setLast(String s) { _last = s; }
			  public String toString() {
				  return "name=>" + _first + " " + _last;
			  }
		}
		private Name _name;
		private boolean _isVerified;
		public Name getName() { return _name; }
		public boolean isVerified() { return _isVerified; }
		public void setName(Name n) { _name = n; }
		public void setVerified(boolean b) { _isVerified = b; }
		
		public String toString() {
			return _name.toString() + "\nverified=>" + _isVerified;
		}
	}
}

Ref: http://jackson.codehaus.org/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值