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

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

1、错误描述

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
	at net.sf.json.AbstractJSON.<clinit>(AbstractJSON.java:53)
	at com.you.model.ListToJSON.main(ListToJSON.java:29)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
	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)
	... 2 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.logging.LogFactory”可以看出,是缺少jar


3、解决办法

   将“commons-logging-1.1.3.jar”复制到lib下

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