I. Using Gson to convert to JSON format
Dependent MAVEN Packages:
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.4</version></dependency>
The key code is as follows:
Import Com.google.gson.gson;import com.google.gson.gsonbuilder;import com.google.gson.jsonobject;//.... Gson Gson = new Gsonbuilder (). Create (); Gson.tojson ("Hello", System.out); Gson.tojson (123, System.out); System.out.println (); Jsonobject json = new Jsonobject (); Json.addproperty ("DD", "22"); Json.addproperty ("DDS", 22); SYSTEM.OUT.PRINTLN (JSON);
Output:
"Hello" 123
{"DD": "$", "DDS": 22}
Second. Convert Json format using json-lib
Json-lib is a Java class library (official website: http://json-lib.sourceforge.net/) to achieve the following functions:
Convert JavaBeans, maps, collections, Java arrays, and XML into JSON format data
. convert JSON format data to a JavaBeans object
Json-lib Required JAR Package
Commons-beanutils-1.8.3.jar
Commons-collections-3.2.1.jar
Commons-lang-2.6.jar
Commons-logging-1.1.1.jar
Ezmorph-1.0.6.jar
Json-lib-2.4-jdk15.jar
Use of Json-lib
1. Parses an Array into a Json string. Use Jsonarray to parse the Array type:
package util.copy;import java.util.arraylist;import java.util.hashset;import java.util.list;import java.util.set;import net.sf.json.jsonarray;import net.sf.json.jsonobject; Public class test { public static void main (String[] args) { JSONObject json2 = new Jsonobject (); json2.put ("ss", ""); system.out.println (Json2); //will Array parsing into Json string string[] str = { "Jack", "Tom", ", " true " }; jsonarray json = jsonarray.fromobject (str); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.ERR.PRINTLN ( JSON); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; //to image arrays, note numbers and cloth values object[] o = { "Beijing", "Shanghai", 89, true, 90.87 }; json = jsonarray.fromobject (o); SYSTEM.ERR.PRINTLN (JSON); //Use collection Class List<String> list = new ArrayList<String> (); list.add ("Jack"); list.add ("Rose"); json = jsonarray.fromobject (list); &NBSP;SYSTEM.ERR.PRINTLN (JSON); //use set set Set<Object> set = new HashSet<Object> (); &nBsp;set.add ("Hello"); set.add (True); set.add, json = jsonarray.fromobject (set); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.ERR.PRINTLN (JSON); }}
The results of the operation are as follows:
{"ss": "55"} ["Jack", "Tom", "All", "true"] ["Beijing", "Shanghai", 89,true,90.87] ["Jack", "Rose"] [99,true, "Hello"]
2. Parse the Javabean/map into a JSON string. Using Jsonobject parsing:
Public static void main (String[] args) { //Analysis HashMap Map<String, Object> map = new HashMap<String, Object> (); map.put ("name", "Tom"); map.put ("Age", 33); jsonobject jsonobject = jsonobject.fromobject (map); system.out.println (Jsonobject); // Parsing javabean person person = new person ("A001", "Jack"); jsonobject = Jsonobject.fromobject (person); System.out.println (Jsonobject); //Parsing nested objects map.put ("person", person); jsonobject = jsonobject.fromobject (map); system.out.println (jsonobject); }
The results of the operation are as follows:
{"Age": $, "name": "Tom"} {"id": "A001", "name": "Jack"} {"Person": {"id": "A001", "name": "Jack"}, "Age": "," "Name": "Tom"}
3. Using the Jsonconfig-over property: for Javabean/map
public static void Main (string[] args) {jsonconfig config = new Jsonconfig (); Config.setexcludes (new string[] {"Name"}); Specifies which properties are not included in the conversion when the person person = new Person ("A001", "Jack"); Jsonobject jsonobject = jsonobject.fromobject (person, config); Pass in the previous configuration object System.out.println (Jsonobject) at the time of conversion; }
The result of the operation is as follows, and we can see that the name attribute is filtered out in the running result:
{"id": "A001"}
4. Convert the Json string to an Array:
public static void Main (string[] args) {Jsonarray Jsonarray = Jsonarray.fromobject ("[89,90,99]"); Object array = Jsonarray.toarray (Jsonarray); SYSTEM.OUT.PRINTLN (array); System.out.println (Arrays.aslist ((object[]) array); }
The results of the operation are as follows:
[Ljava.lang.object;@1e5003f6[89, 90, 99]
5. Turn the Json string into Javabean/map:
Public static void main (String[] args) { //convert strings in Json form to Map String str = "{\" name\ ": \" tom\ ", \" age\ ":"; " jsonobject jsonobject = jsonobject.fromobject ( STR); map<string, object> map = (map<string, object>) jsonobject.tobean (jsonobject, map.class); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (map); //will string conversion to javabean str in Json form = "{\" id\ ": \" A001\ ", \" name\ ": \" Jack\ "}"; jsonobject = jsonobject.fromobject (str); System.out.println (Jsonobject); person person = (person) jsonobject.tobean (jsonobject, person.class); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (person); }
The results of the operation are as follows:
{age=90, Name=tom} person [id=a001, Name=jack]
When converting a Json-like string to JavaBean, it is important to note that there must be no parameter constructor in the JavaBean, or the following error cannot be found for the initialization method:
Exception in thread "main" Net.sf.json.jsonexception:java.lang.nosuchmethodexception:cn.sunzn.json.person.<init > () at Net.sf.json.JSONObject.toBean (jsonobject.java:288) at Net.sf.json.JSONObject.toBean (jsonobject.java:233) At Cn.sunzn.json.JsonLib.main (jsonlib.java:23) caused By:java.lang.NoSuchMethodException:cn.sunzn.json.Person. <init> () at Java.lang.Class.getConstructor0 (Unknown Source) at Java.lang.Class.getDeclaredConstructor (Unknown Source) at Net.sf.json.util.newbeaninstancestrategy$defaultnewbeaninstancestrategy.newinstance ( newbeaninstancestrategy.java:55) at Net.sf.json.JSONObject.toBean (jsonobject.java:282) ... 2 more
This article from the "10916470" blog, reproduced please contact the author!
Convert data to JSON format