https://github.com/FasterXML/jackson-databind/
public class MyValue1 {
private String f1;
public MyValue1() {
super();
}
public MyValue1(String f1) {
super();
this.f1 = f1;
}
public String getF1() {
return f1;
}
public void setF1(String f1) {
this.f1 = f1;
}
}
-----------------------
import java.util.List;
public class MyValue {
public String name;
public int age;
public List<MyValue1> myvalue1s;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public MyValue() {
// TODO Auto-generated constructor stub
}
public MyValue(String name, int age) {
super();
this.name = name;
this.age = age;
}
public List<MyValue1> getMyvalue1s() {
return myvalue1s;
}
public void setMyvalue1s(List<MyValue1> myvalue1s) {
this.myvalue1s = myvalue1s;
}
}
---------------------------
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
*
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
*/
public class JackJsonCollection {
public void test() throws Exception {
ObjectMapper mapper = new ObjectMapper();
//json 转 map
System.out.println(" ------------------------------json 转 map------------------------------");
Map<String, Integer> scoreByName = mapper.readValue("{\"name\":88, \"age\":13}", Map.class);
System.out.println(scoreByName.get("name"));
System.out.println(scoreByName.get("age"));
//map 转 json
System.out.println(" ------------------------------map 转 json------------------------------");
System.out.println(mapper.writeValueAsString(scoreByName));
//json 转 list
System.out.println(" ------------------------------json 转 list------------------------------");
List<String> lists = mapper.readValue("[\"00\",\"11\"]", List.class);
System.out.println(lists.get(0));
System.out.println(lists.get(1));
//list 转 json
System.out.println(" ------------------------------list 转 json------------------------------");
System.out.println(mapper.writeValueAsString(lists));
//复杂类转换
System.out.println(" ------------------------------复杂类转换------------------------------");
Map<String, MyValue> results1 = new HashMap<String, MyValue>();
MyValue v0 = new MyValue("n0", 0);
List<MyValue1> myvalue0s = new ArrayList<MyValue1>();
myvalue0s.add(new MyValue1("f0"));
v0.setMyvalue1s(myvalue0s);
results1.put("0", v0);
MyValue v1 = new MyValue("n1", 0);
List<MyValue1> myvalue1s = new ArrayList<MyValue1>();
myvalue1s.add(new MyValue1("f1"));
v1.setMyvalue1s(myvalue1s);
results1.put("1", v1);
String jsonSource = mapper.writeValueAsString(results1);
System.out.println(jsonSource);
System.out.println(" ------------------------------复杂类转换------------------------------");
Map<String, MyValue> results = mapper.readValue(jsonSource,
new TypeReference<Map<String, MyValue>>() { } );
System.out.println(results.get("0").getMyvalue1s().get(0).getF1());
System.out.println(results);
}
public static void main(String[] args) throws Exception {
JackJsonCollection jack = new JackJsonCollection();
jack.test();
}
}