下载jackson包后,即可自动实现。
package onlearn.neuedu.onlearn;import java.io.IOException;import org.junit.Test;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import com.neuedu.onlearn.web.ApiResult;public class TeacherTest {@Testpublic void testJackon() throws IOException {ObjectMapper objMapper = new ObjectMapper();ApiResult apiRs = new ApiResult();apiRs.setCode(200);apiRs.setMsg("success");//java对象转换为jsonString rs = objMapper.writeValueAsString(apiRs);System.out.println(rs);//json转换为java对象ApiResult src = objMapper.readValue(rs, ApiResult.class);System.out.println(src);}}