一、引入的jar包
json-lib-2.4-jdk15.jar
二、Json字符串转换为其他对象
1、对象==》json字符串 2、list和Map集合==》json字符串 3、Map集合==》json字符串【具体代码如下】
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap; import com.coscon.ferightEntrust.vo.CdViewVo; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class TestCd {
public static void main(String[] args) {
collectToStr();
} private static void collectToStr(){ CdViewVo cvv=new CdViewVo();
cvv.setCntrNum("CBHU898637"); cvv.setSealNum("CBHU898637");
cvv.setCntrType("20Gp"); cvv.setObTrafficTerm("jkd");
cvv.setIbTrafficTerm("P-预付"); cvv.setPackDesc("");
cvv.setGrossWt(""); cvv.setVol("54.23"); CdViewVo cvv2=new CdViewVo();
cvv2.setCntrNum("CBHU893437"); cvv2.setSealNum("CBHU893437");
cvv2.setCntrType("40Gp"); cvv2.setObTrafficTerm("bhc");
cvv2.setIbTrafficTerm("P-预付"); cvv2.setPackDesc("");
cvv2.setGrossWt(""); cvv2.setVol("84.23");
//Java对象
JSONObject cvvo=JSONObject.fromObject(cvv);
String cvvs=cvvo.toString();
System.out.println("CdViewVo的json字符串:"+cvvs);
//map集合
Map<String, String> tm=new TreeMap<String, String>();
tm.put("a", "ddd"); tm.put("b", "ccc");
tm.put("c", "bbb"); tm.put("d", "aaa");
JSONArray array=JSONArray.fromObject(tm);
String tmstr=array.toString();
System.out.println("Map集合对应的json字符串:"+tmstr); //list集合
List<CdViewVo> list=new ArrayList<CdViewVo>();
list.add(cvv);
list.add(cvv2);
JSONArray listArr=JSONArray.fromObject(list);
String listStr=listArr.toString();
System.out.println("listStr集合对应的json字符串:"+listStr);
}
}
运行结果如下:
CdViewVo的json字符串:{"cntrNum":"CBHU898637","cntrType":"20Gp","grossWt":"","ibTrafficTerm":"P-预付","obTrafficTerm":"jkd","packDesc":"","packNum":"","sealNum":"CBHU898637","vol":"54.23"}
Map集合对应的json字符串:[{"a":"ddd","b":"ccc","c":"bbb","d":"aaa"}]
listStr集合对应的json字符串:[{"cntrNum":"CBHU898637","cntrType":"20Gp","grossWt":"","ibTrafficTerm":"P-预付","obTrafficTerm":"jkd","packDesc":"","packNum":"","sealNum":"CBHU898637","vol":"54.23"},
{"cntrNum":"CBHU893437","cntrType":"40Gp","grossWt":"","ibTrafficTerm":"P-预付","obTrafficTerm":"bhc","packDesc":"","packNum":"","sealNum":"CBHU893437","vol":"84.23"}]
三、Java对象转换为Json字符串
import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class TestCd {
public static void main(String[] args) {
jsonStrToJava();
}
private static void jsonStrToJava(){
String jsonStr="{\"cntrNum\":\"CBHU898637\",\"cntrType\":\"20Gp\",\"grossWt\":\"100\"," +
"\"ibTrafficTerm\":\"P-预付\",\"obTrafficTerm\":\"jkd\",\"packDesc\":\"12\"," +
"\"packNum\":\"\",\"sealNum\":\"CBHU898637\",\"vol\":\"54.23\"}";
JSONObject java=JSONObject.fromObject(jsonStr);
CdViewVo cvv=(CdViewVo) JSONObject.toBean(java,CdViewVo.class);
System.out.println("cntrNum: "+cvv.getCntrNum());
}
}
运行结果:
cntrNum: CBHU898637
四、集合转换为Json字符串
String str = "{\"lendperson\":\"李四\",\"lendcompany\":\"有限公司\",\"checkperson\":\"李四\",
\"lenddate\":\"2010-07-19T00:00:00\",\"lendcounts\":4,\"
passports\":[{\"passportid\":\"d\",\"name\":\"李豫川\",\"passporttype\":\"K\"},
{\"passportid\":\"K9051\",\"name\":\"李平\",\"passporttype\":\"K\"},
{\"passportid\":\"K90517\",\"name\":\"袁寒梅\",\"passporttype\":\"K\"},
{\"passportid\":\"K905199\",\"name\":\"贺明\",\"passporttype\":\"K\"}]}"; JSONObject jsonobject = JSONObject.fromObject(str);
PassportLendsEntity passportlends = null;
try {
//获取一个json数组
JSONArray array = jsonobject.getJSONArray("passports");
//将json数组 转换成 List<PassPortForLendsEntity>泛型
List<PassPortForLendsEntity> list = new ArrayList<PassPortForLendsEntity>();
for (int i = ; i < array.size(); i++) {
JSONObject object = (JSONObject)array.get(i);
PassPortForLendsEntity passport = (PassPortForLendsEntity)JSONObject.toBean(object,
PassPortForLendsEntity.class); //转换成Java对象
if(passport != null){
list.add(passport);
}
}