Error in converting List to JSON in Java (2), javajson

Source: Internet
Author: User

Error in converting List to JSON in Java (2), javajson

1. Error description

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactoryat 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.LogFactoryat 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. Error cause

/*** Convert List to 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 ("Spring"); list. add ("Summer"); list. add ("Autumn"); list. add ("Winter"); JSONArray jsonArray = JSONArray. fromObject (list); System. out. println ("List converted to JSON:" + jsonArray. toString ());}}

We can see from "Caused by: java. lang. ClassNotFoundException: org. apache. commons. logging. LogFactory" that jar is missing.


3. Solution

Copy commons-logging-1.1.3.jar to lib


How to convert list into json in java

// Need to use json-lib.jar, you can download here: json-lib.sourceforge.net/
// Code instance
List <String> list = new ArrayList <String> ();
List. add ("abc ");
List. add ("123 ");

Net. sf. json. JSONArray jsonArray = net. sf. json. JSONArray. fromObject (list );
System. out. println (jsonArray. toString ());

Conversion from List set to json in java

Exception has told you, obviously the package is missing, should be missing the commons-beanutils.jar File

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.