测试代码
import com.google.gson.Gson;
import org.junit.Test;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
/**
* @author hongjie.li@boolin.cn
*/
public class JsonTest {
@Test
public void json() throws Exception {
HashMap<String, Object> map = new HashMap<>();
List<Person> personList = Arrays.asList(new Person("test1", 1), new Person("test2", 2));
map.put("persons", personList);
System.out.println("Nutz Json:" + Json.toJson(map, JsonFormat.compact()));
System.out.println("Gson Json:" + new Gson().toJson(map));
}
private class Person {
private final String name;
private final int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
}
[DEBUG] 11:46:40.325 org.nutz.castor.Castors.reload(Castors.java:115) - Using 95 castor for Castors
Nutz Json:{"persons":[{}, {}]}
Gson Json:{"persons":[{"name":"test1","age":1},{"name":"test2","age":2}]}