Use of json packages in java and conversion between strings, map, list, custom objects, and jsonmap

Source: Internet
Author: User
Tags package json

Use of json packages in java and conversion between strings, map, list, custom objects, and jsonmap

To convert a map and a string, you need to import these jar packages, which are the most basic jar packages.

After multiple parties try to draw a conclusion:

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

Without this jar package, the program cannot be written.

Now we have ensured that the program can be compiled and run it.

Exception: org/apache/commons/lang/exception/NestableRuntimeException

Import commons-lang-2.3.jar to solve the problem.

Run:

Exception: java. lang. ClassNotFoundException: net. sf. ezmorph. Morpher

Importing ezmorph-1.0.3.jar, problem solving

Run:

Exception: java. lang. ClassNotFoundException: org. apache. commons. logging. LogFactory

Importing commons-logging-1.1.1.jar, problem solving

Run:

Exception: java. lang. NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap

Importing commons-collections-3.2.1.jar, problem solving

Run:

Exception: java. lang. NoClassDefFoundError: org/apache/commons/beanutils/DynaBean

Importing commons-beanutils-1.7.0.jar, problem solving

Run it. The exception is gone.

Let's take a look at how these things are converted.

Map to string

Map <String, String> map = new HashMap <String, String> (); map. put ("name", "Dr. Sisi"); map. put ("age", "25 ");
JSONObject jsonObject = JSONObject. fromObject (map); String res = jsonObject. toString (); System. out. println (res );

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

List to string

List <String> list = new ArrayList <String> (); list. add ("China"); list. add ("name"); list. add ("Republic"); JSONArray jsonArray = JSONArray. fromObject (list); String res = jsonArray. toString (); System. out. println (res );

Result: ["China", "People's name", "Republic"]
String to map

String res = "{\" age \ ": \" 25 \ ", \" name \ ": \" Dr. Sisi \"}"; // or like this // res = "{'age': '25', 'name': 'siss'}"; 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 ());

Result:

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

String to our custom class

UserBean. java

package json;public class UserBean {    private String name;    private String age;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getAge() {        return age;    }    public void setAge(String age) {        this.age = age;    } }
String userS = "{'name': 'siss', 'age': '25'}"; JSONObject obj = JSONObject. fromObject (userS); UserBean userBean = (UserBean) JSONObject. toBean (obj, UserBean. class); System. out. println (userBean. getName ());

String 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]);

Result:

3
Ccc

 

Related Article

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.