The use of JSON packages in Java and the conversion of strings, Map,list, and custom objects to each other

Source: Internet
Author: User

To do a map and string conversion, you need to import these jar packages, which are some of the most basic jar packages.

After multiple attempts to draw conclusions into:

First import the basic package: Json-lib-2.2.3-jdk15.jar

Without this jar package, the program is not writable.

Now it's guaranteed that the program will compile and run.

Exception occurred: org/apache/commons/lang/exception/nestableruntimeexception

Import Commons-lang-2.3.jar, problem solved.

Run for a moment:

Exception occurred: Java.lang.ClassNotFoundException:net.sf.ezmorph.Morpher

Import Ezmorph-1.0.3.jar, problem solving

Run for a moment:

Exception occurred: Java.lang.ClassNotFoundException:org.apache.commons.logging.LogFactory

Import Commons-logging-1.1.1.jar, problem solving

Run for a moment:

Exception occurred: Java.lang.noclassdeffounderror:org/apache/commons/collections/map/listorderedmap

Import Commons-collections-3.2.1.jar, problem solving

Run for a moment:

Exception occurred: Java.lang.noclassdeffounderror:org/apache/commons/beanutils/dynabean

Import Commons-beanutils-1.7.0.jar, problem solving

Run, the exception is gone.

Let's see how these things are converted.

Map Turn string

Map<string, string> map=new hashmap<string, string>(); Map.put ("name", "Dr. Sisi"); Map.put ("age", "+");
jsonobject jsonobject=jsonobject. Fromobject (map); String res=jsonobject. toString (); System.out.println (res);

Result: {"age": "+", "name": "Dr. Sisi"}

List Turn string

list<string> list=New arraylist<string>() list.add ("Zhonghua"); List.add ("name" ); List.add ("Republic");        Jsonarray Jsonarray=jsonarray.fromobject (list); String res=jsonarray.tostring (); System.out.println (res);

Results: ["China", "name", "Republic"]
String goto Map

String res= "{\" age\ ": \" 25\ ", \" name\ ": \" Sisi dr \ "}"; // or so // res= "{' Age ': ' + ', ' name ': ' Dr. Sisi '}"; Jsonobject jsonobject=Jsonobject.fromobject (res); Map<String,String> map = (hashmap<string,string>) Jsonobject.tobean (Jsonobject, HashMap.  Class); SYSTEM.OUT.PRINTLN (map); System.out.println (Map.size ()); System.out.println (Map.get ("name")); System.out.println (Map.getclass (). GetName ());

Results:

{Dr. Name= Sisi, age=25}
2
Dr. Sisi
Java.util.HashMap

String into our custom class

Userbean.java

 PackageJSON; Public classUserBean {PrivateString name; PrivateString age;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString getage () {returnAge ; }     Public voidsetage (String age) { This. Age =Age ; } }
String UserS = "{' name ': ' Dr. Sisi ', ' age ': '+ ' == (UserBean) jsonobject.tobean (obj, UserBean.  Class); System.out.println (Userbean.getname ());

String converted to List

String arraystring= "[' A ', ' B ', ' CCC ']"; Jsonarray Jsonarray=jsonarray.fromobject (arraystring); object[] arr=jsonarray.toarray (); System.out.println (arr.length); System.out.println (arr[2]);

Results:

3
Ccc

The use of JSON packages in Java and the conversion of strings, Map,list, and custom objects to each other

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.