体验了一把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/