JAVA对象、数组转JSON 1.需要库:
json转换对象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 = String();
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 = String();
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"]

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。