maven添加json需要的依赖

天下午在Maven项目中使用JSON的时候遇到了很多问题,一开始引入包就直接百度,也没注意POM.XML中的错误,然后就开始写代码,害得我认为是我的代码写错了,弄了一下午,百度也没有比较全的依赖包的资料,在此记录下。

以下是使用JSON需要进入的5个依赖包

net.sf.json-lib json-lib 2.4 jdk15 commons-beanutils commons-beanutils 1.7.0 commons-collections commons-collections 3.1 commons-lang commons-lang 2.5 net.sf.ezmorph ezmorph 1.0.3
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
找了很久 的 终于搞好了 JSON数据以来包全部的jar包 解压后放在项目下就可以了 下面付源代码 import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class Json { public static void main(String[] args) { Json j = new Json(); j.bean2json(); } public void arr2json() { boolean[] boolArray = new boolean[] { true, false, true }; JSONArray jsonArray = JSONArray.fromObject(boolArray); System.out.println(jsonArray); // prints [true,false,true] } public void list2json() { List list = new ArrayList(); list.add("first"); list.add("second"); JSONArray jsonArray = JSONArray.fromObject(list); System.out.println(jsonArray); // prints ["first","second"] } public void createJson() { JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']"); System.out.println(jsonArray); // prints ["json","is","easy"] } public void map2json() { Map map.put("name", "json"); map.put("bool", Boolean.TRUE); map.put("int", new Integer(1)); map.put("arr", new String[] { "a", "b" }); map.put("func", "function(i){ return this.arr[i]; }"); JSONObject json = JSONObject.fromObject(map); System.out.println(json); // prints // ["name":"json","bool":true,"int":1,"arr":["a","b"],"func":function(i){ // return this.arr[i]; }] } public void bean2json() { JSONObject jsonObject = JSONObject.fromObject(new MyBean()); System.out.println(jsonObject); } public void json2bean() { String json = "{name=\"json2\",func1:true,pojoId:1,func2:function(a){ return a; },options:['1','2']}"; JSONObject jb = JSONObject.fromString(json); JSONObject.toBean(jb, MyBean.class); System.out.println(); } } 操作的bean: import net.sf.json.JSONFunction; public class MyBean { private String name = "json"; private int pojoId = 1; // private char[] options = new char[] { 'a', 'f' }; private String func1 = "function(i){ return this.options[i]; }"; private JSONFunction func2 = new JSONFunction(new String[] { "i" }, "return this.options[i];"); // getters & setters ...... }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值