今天我想把一个字符串类型转换为JSON数组格式,但总是报java.lang.noclassdeffounderror的错误,最终我把这个问题解决了,下面说一下解决的步骤
一、首先是的引包
要想程序进行必须引入json-lib jar 包,引入这个是不够的,必须引入以下的其他包
1、commons-beanutils.jar
2、commons-collections-3.2.jar
3、commons-lang-2.5.jar
4、commons-logging-1.1.1.jar
5、ezmorph-1.0.4.jar
6、json-lib-2.4-jdk15.jar
引入这些包才能保证缩写代码的正常运行
二、具体的应用(JSONArray的运用)
package zyj.com;
import java.util.List;
import net.sf.json.JSONArray;
public class JsonTest {
public static void main(String[] args) {
JSONArray jsonObject = JSONArray.fromObject("[{'name':'huangbiao','age':15},{'name':'liumei','age':14}]");
List<StudentPo> student=jsonObject.toList(jsonObject,StudentPo.class);
for (StudentPo studentPo : student) {
System.out.println(studentPo.getName()+";"+studentPo.getAge());
}
}
}
结果:
huangbiao;15
liumei;14
三、JSONArray和JSONObject的区别
JSONObject是一个{}包裹起来的一个对象;
JSONArray是用[ ]包裹起来的数组;