JSON is JavaScript Object Natation, which is a lightweight data exchange format that is very suitable for the interaction between servers and JavaScript. This article will quickly explain the JSON format and use code examples to demonstrate how to process JSON format data on the client and server side respectively.
Required packages for Json:
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
The java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher error occurs because the ezmorph.jar file is not imported or the version is incorrect.
The 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 java collection json collections
1. Convert Java collections to Json collections
Key class: JSONArray jsonArray = JSONArray.fromObject(Object obj);
Instructions for use: Pass the Java collection object directly into JSONArray.fromObject() to get a JSONArray collection, and then use the toString() method of JSONArray to get the json collection
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 json collection JSONArray jsonArray = JSONArray.fromObject(list); System.out.println(jsonArray.toString()); }Output result:
[{"age":"13","name":"zhangSan"},{"age":"14","name":"liSi"}]2. Convert Json collection to Java collection
Key class: JSONArray jsonArray = JSONArray.fromObject(Object obj);
Instructions for use: Pass the json string object in, you can get a JSONArray object, and then call the toCollection (JSONArray jsonArray, Class clss) method of the JSONArray object to get 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 employee : list){ System.out.println(employee); } }Output result:
[name=zhangSan,age=13][name=liSi,age=14]
2.JAVA implements the method of converting XML and JSON to each other
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(); // Root node name xmlSerializer.setRootName("xml"); // Don't set the type xmlSerializer.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 above article on Java's various conversion methods (recommended) are all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.