Various conversion methods (recommended) for processing json in java, javajson
JSON is JavaScript Object Natation. It is a lightweight data exchange format and is very suitable for server-to-JavaScript interaction. This article will quickly explain the JSON format and demonstrate how to process JSON format data on the client and server through code examples.
Required Json package:
Commons-httpclient-3.1.jar
Commons-lang-2.4.jar
Commons-logging-1.1.1.jar
Json-lib-2.2.3-jdk13.jar
Ezmorph-1.0.6.jar
Commons-collections-3.2.1.jar
Java. lang. NoClassDefFoundError: net/sf/ezmorph/Morpher error occurs because the ezmorph. jar file or version is not imported.
Java. lang. NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap error occurs because the commons-collections.jar file is not imported or the version is incorrect.
1. Conversion between json sets of java Sets
1. convert a Java set to a Json set
Key class: JSONArray jsonArray = JSONArray. fromObject (Object obj );
Instructions for use: directly upload the Java Collection object to JSONArray. fromObject () to obtain a JSONArray set, and then directly use the toString () method of JSONArray to obtain the json set.
Sample Code:
@ Test public void testCreateJsonArray () {// Java Collection List <Employee> list = new ArrayList <Employee> (); list. add (new Employee ("zhangSan", "13"); list. add (new Employee ("liSi", "14"); // create a json set JSONArray jsonArray = JSONArray. fromObject (list); System. out. println (jsonArray. toString ());}
Output result:
[{"age":"13","name":"zhangSan"},{"age":"14","name":"liSi"}]
2. convert a Json set to a Java set
Key class: JSONArray jsonArray = JSONArray. fromObject (Object obj );
Instructions for use: pass in a json String object to obtain a JSONArray object. Then, call the toCollection (JSONArray, Class clss) method of the jsonArray object to obtain a collection of Java objects.
Sample Code:
@ Test public void testParseJsonArray () {// json collection String jsonString = "[{\" age \ ": \" 13 \ ", \" name \": \ "zhangSan \" },{ \ "age \": \ "14 \", \ "name \": \ "liSi \"}] "; JSONArray jsonArray = JSONArray. fromObject (jsonString); // Java Collection List <Employee> list = (List <Employee>) jsonArray. toCollection (jsonArray, Employee. class); for (Employee: list) {System. out. println (employee );}}
Output result:
[name=zhangSan,age=13][name=liSi,age=14]
2. How to convert XML and JSON in JAVA
1. Convert XML to Json
public static String xmlToJson(String xml) {XMLSerializer serializer = new XMLSerializer();return serializer.read(xml).toString();}
2. Convert JSON to XML
Public static String jsonToXML (String json) {XMLSerializer xmlSerializer = new XMLSerializer (); // The root node name xmlSerializer. setRootName ("xml"); // The xmlSerializer type is not set. setTypeHintsEnabled (false); String xmlStr = ""; if (json. contains ("[") & json. contains ("]") {// jsonArray JSONArray jobj = JSONArray. fromObject (json); xmlStr = xmlSerializer. write (jobj);} else {// jsonObject JSONObject jobj = JSONObject. fromObject (json); xmlStr = xmlSerializer. write (jobj);} System. out. println ("converted parameters:" + xmlStr); return xmlStr ;}
The various conversion methods (recommended) for processing json in the above java section are all the content shared by Alibaba Cloud xiaobian. I hope to give you a reference and support for the customer's house.