-
对象或List进行JSON转化:
String jsonStr = JSON.toJSONString(tableInfoVO.getFieldInfo());
2. JSON字符串转化为对象:
https://www.cnblogs.com/dianzan/p/11332672.html
如下两个例子:
JSONObject jsonobject = JSON.parseObject(str);
或者:
JSONObject jsonobject = JSONObject.parseObject(str);
String s = httpRequest.sendGet("https://api.weixin.qq.com/sns/oauth2/access_token","appid=" + appId + "&secret=" + appSecret + "&code=" + code + "&grant_type=authorization_code"); UserAuthorizationReturn userAuthorizationReturn = JSON.parseObject(s, UserAuthorizationReturn.class);
具体例子:
public class Staff { private String name; private int age; private String sex; private Date birthday;
}
public static void main(String args[]) { //json字符串转化为对象 String jsonString = "{name:‘yxs‘,age:23,sex:‘man‘,telephone:‘12346‘}"; Staff staff = JSON.parseObject(jsonString, Staff.class); System.out.println(staff.toString()); //对象转化为json字符串 String string = JSON.toJSONString(staff); System.out.println(string); }
//输出结果
Staff{name=‘yxs‘, age=23, sex=‘man‘, birthday=null}
{"age":23,"name":"yxs","sex":"man"}
总结:在JSON。parseObject的时候,回去填充名字相同的属性,对于Json字符串中没有,而model类中有的属性,会为null,对于model类中没有,而json字符串中有的属性,不做任何处理
3. JSON字符串转化为List:
String jsonStr = JSON.toJSONString(tableInfoVO.getFieldInfo()); List<FiledInfoVO> filedInfoVOList = JSON.parseArray(jsonStr,FiledInfoVO.class);