Conversion between List and Json, and between ListJson

Source: Internet
Author: User

Conversion between List and Json, and between ListJson

List and Json conversion require six jar packages. If you do not reference these jar packages, you need to write a large amount of code, here, the conversion using the jar package is recorded temporarily.

The following figure shows the structure of the test demo. It is for reference only. Pay attention to the version of the jar package.

 

 

The code in Test. java is as follows:

Package com. listandjson; import java. util. arrayList; import java. util. list; import net. sf. json. JSONArray; public class Test {public static void main (String [] args) {List <String> list = new ArrayList <String> (); list. add ("abc"); list. add ("123"); // convert list to jsonString json = JSONArray. fromObject (list ). toString (); System. out. println (json); // run: ["abc", "123"] // convert json to list JSONArray jsonArray = JSONArray. fromObject (json); List <String> list2 = (List) JSONArray. toCollection (jsonArray); for (int I = 0; I <list2.size (); I ++) {System. out. println (list2.get (I); // run: abc // 123 }}}

 

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.