Suppose we have obtained a Java object on the server side, and now we are considering how to convert this Java object into a string that complies with the JSON syntax. Of course, we can write a set of algorithms to implement this conversion, but it is troublesome, especially when the object has many attributes or the a
First we introduce the next map and object conversion code.
The specific code looks like this:
/** * Use org.apache.commons.beanutils for conversion/class A {public static Object Maptoobject (map
Here's a brief introduction to the transformation of map and JSON
First paragraph code
Recently, the entire transmission of data, of course, has been in the JSON format, naturally, the conversion between the format of the data, including JSON to Java Bean object, package parsing map, list and so on. Today we'll probably record these methods for later use.One, JSON1.
);}
The code above converts a JSON string to a custom complex data entity in Java. Complexuser: Check the code in the last appendix.
3. Convert JSON data to list in Java
/*** Convert the list data encapsulated in
1. Array goto list:string[] countries = {"Array", "List", "Set", "Map"};List List = arrays.aslist (countries);2.Array turn : SetString [] countries = {"Array", "List", "Set"};setSYSTEM.OUT.PRINTLN (set);3. List to array:listList.add ("Array");List.add ("List");List.add ("Set");List.add ("Map");String [] countries = List.toarray (New String[list.size ()));4. Map t
Original: Share very useful Java program (key Code) (V)---convert Array to MapImport Java.util.Map; Import Org.apache.commons.lang.ArrayUtils; public class Main {public static void Main (string[] args) { string[][] countries = {"states", "New York "}, {" Kingdom "," London "}, {" Netherland "," Amsterdam "}, {" Japan "," Tokyo "}, {" France "," Paris "}};
In Java, we often need to do some conversion to list, array, etc., of course there are many kinds of conversion methods, but which method is both convenient and efficient? Let me introduce you to the best conversion method between sets.convert 1.List to arraylist2.Array Conversion to Liststring[] countries = {"China", "Switzerland", "Italy", "France"}; List List = arrays.aslist (countries);3.Map Conversion
Use JSON-lib to convert a Java object to a JSON string
After learning to use Ajax in J2EE, sometimes the client data returned from the server is an object. It is easy to make errors when you manually piece together the JSON string. Google knows the
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
I. General description
XML and JSON are the most common data exchange formats. This example shows how to convert a java object to JSON output.
Ii. Process
1. In the project above,
Add the code in "com. waylau. rest. resources. UserResource". The Code is as follows:
@GET @Path("/getUserJson") @Produces(Me
Convert xml to json objects in java
1. A jar package is required:Json-lib-2.4-jdk15.jar and xom-1.2.5.jar, maven Repository:Net. sf. json-libJson-lib2.4Jdk15XomXom1.2.52. Code:Public static JSON xmlToJson (String xml ){XMLSerializer xmlSerializer = new XMLSerializer ();If (
1. Import the Jackson jarDependency> groupId>Com.fasterxml.jackson.coregroupId> Artifactid>Jackson-databindArtifactid> version>2.8.9version>Dependency>2. Java code is as follows Public classJsonutils {//defining the Jackson object Private Static FinalObjectmapper MAPPER =NewObjectmapper (); /*** Converts an object into a JSON string. * @paramData *@return */ Public StaticString Objecttoj
Currently, it is quite simple to convert a Java object to a JSON object. However, it is more complicated to convert a JSON object to a Java object.
The first method is JSON-lib.
The sec
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.