JAVA对象数组转JSON

Posted zzlei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA对象数组转JSON相关的知识,希望对你有一定的参考价值。

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"]

 





以上是关于JAVA对象数组转JSON的主要内容,如果未能解决你的问题,请参考以下文章

java中把json怎么转换成数组?

java对象怎么转json数组 跪求啊

json数组转java对象怎么转?

json数组转java对象怎么转

java对象怎么转成json数组 传到前台页面

怎么将json对象添加进json数组中