首页 > 代码库 > Java中的List转换成JSON报错(四)

Java中的List转换成JSON报错(四)

1、错误描述

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap
	at net.sf.json.JSONObject.<init>(JSONObject.java:1450)
	at net.sf.json.util.CycleDetectionStrategy.<clinit>(CycleDetectionStrategy.java:37)
	at net.sf.json.JsonConfig.<clinit>(JsonConfig.java:65)
	at net.sf.json.JSONArray.fromObject(JSONArray.java:105)
	at com.you.model.ListToJSON.main(ListToJSON.java:29)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.map.ListOrderedMap
	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
	... 5 more

2、错误原因

/**
 * 将List转换成JSON
 */
package com.you.model;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

/**
 * @author YouHaiDong
 *
 */
public class ListToJSON 
{

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		List<String> list = new ArrayList<String>();
		list.add("春天");
		list.add("夏天");
		list.add("秋天");
		list.add("冬天");
		
		JSONArray jsonArray = JSONArray.fromObject(list);
		
		System.out.println("List转换成JSON:" + jsonArray.toString());
	}

}

从“Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.map.ListOrderedMap”可以看出,缺少jar


3、解决办法

   将“commons-collections-3.1.jar”复制到lib目录下

Java中的List转换成JSON报错(四)