日期:2014-05-16 浏览次数:20618 次
public static class User { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } User user = new User(); user.setId(123); user.setName("毛头"); JSONSerializerMap mapping = new JSONSerializerMap(); mapping.put(User.class, new JavaBeanSerializer(User.class, Collections.singletonMap("id", "uid"))); JSONSerializer serializer = new JSONSerializer(mapping); serializer.write(user); String jsonString = serializer.toString(); Assert.assertEquals("{\"uid\":123}", jsonString);
public class JsonResult { private String message; private List results; public List getResults() { return results; } public void setResults(List obj) { this.results = obj; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
int[] intArr = new int[] { 4, 2 }; String jsonstr = JsonWrap.beanToJsonString_2(intArr); System.out.println(jsonstr);