1.需要库:
commons-beanutils-1.7.0.jar
commons-collections-3.1.jar
commons-lang-2.1.jar
commons-logging-1.1.3.jar
ezmorph-1.0.2.jar
json-lib-2.4-jdk15.jar
2.转换方法:
//对象转json
sqlModel model = new sqlModel();
model.setAge(18);
model.setHeight(188.0);
model.setName("张三");
String jsonStr = null;
try {
JSONObject jsonObject = JSONObject.fromObject(model);
jsonStr = jsonObject.toString();
System.out.println(jsonStr);
} catch (Exception e) {
System.out.println("数组转json失败");
}
结果:
{"age":18,"height":188,"name":"张三"}
//数组转json
String jsonStr = null;
try {
JSONArray jsonObject = JSONArray.fromObject(myNameList);
jsonStr = jsonObject.toString();
System.out.println(jsonStr);
} catch (Exception e) {
System.out.println("数组转json失败");
}
结果:["aaaa","bbbb","cccc","cc","zzl","zzl","zzl","zzl","zzl","zzl","zzl","zzl","zzl","zzl","zzl","zzl","zzl","zzl"]
参考地址 https://www.cnblogs.com/z-z-z/p/6839356.html
今天的文章JAVA对象、数组转JSON分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/7496.html