今天我想把一个字符串类型转换为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是用[ ]包裹起来的数组;
本文详细介绍了在Java中将字符串转换为JSON数组并利用JSONArray进行操作的方法,包括必需的依赖库引入、具体应用示例以及JSONArray与JSONObject的区别。通过实例演示,帮助开发者解决实际开发中遇到的问题。
&spm=1001.2101.3001.5002&articleId=48999717&d=1&t=3&u=2a1e2e8f131e4975ad7e391c71057869)
18万+

被折叠的 条评论
为什么被折叠?



