The various conversion methods in java for handling json are recommended by of

  • 2020-12-05 17:11:23
  • OfStack

JSON, or JavaScript Object Natation, is a lightweight data exchange format that is well suited for server interactions with JavaScript. This article takes a quick look at the JSON format and shows you how to process JSON data on the client side and the server side, respectively, with code examples.

Json required packages:

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

Appear java. lang. NoClassDefFoundError: net/sf/ezmorph/Morpher error because no import ezmorph jar file or the wrong version.

Appear java. lang. NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap error because no import commons - collections. jar file or the wrong version.

1. Conversion between java sets and json sets

1. Java set converted to Json set

Key class: JSONArray jsonArray = ES61en. fromObject(Object obj);

Instruction: the Java collection object is directly passed into JSONArray.fromObject (), and one JSONArray collection is obtained. Then, the toString() method of JSONArray is directly used to obtain the json collection

Sample code:


@Test
  public void testCreateJsonArray() {
    //Java A collection of 
    List<Employee> list = new ArrayList<Employee>();
    list.add(new Employee("zhangSan","13"));
    list.add(new Employee("liSi","14"));
    // create json A collection of 
    JSONArray jsonArray = JSONArray.fromObject(list);
    System.out.println(jsonArray.toString());
  }

Output results:


[{"age":"13","name":"zhangSan"},{"age":"14","name":"liSi"}]

2. Json set converted to Java set

Key class: JSONArray jsonArray = ES88en. fromObject(Object obj);

Instruction: Pass in json string object, you can get 1 JSONArray object, and then call toCollection(JSONArray jsonArray, Class clss) method of JSONArray object, you can get 1 Java object collection.

Sample code:


@Test
  public void testParseJsonArray() {
    //json A collection of 
    String jsonString = "[{\"age\":\"13\",\"name\":\"zhangSan\"},{\"age\":\"14\",\"name\":\"liSi\"}]";
    
    JSONArray jsonArray = JSONArray.fromObject(jsonString);
    //Java A collection of 
    List<Employee> list = (List<Employee>) jsonArray.toCollection(jsonArray, Employee.class);
    for(Employee employee : list){
      System.out.println(employee);
    }
  }

Output results:


[name=zhangSan,age=13]
[name=liSi,age=14]

2.JAVA realizes the mutual conversion method between XML and JSON

1.XML converted to Json


public static String xmlToJson(String xml) {
XMLSerializer serializer = new XMLSerializer();
return serializer.read(xml).toString();
}

2.JSON converted to XML


public static String jsonToXML(String json) {
  XMLSerializer xmlSerializer = new XMLSerializer();
  //  Root node name 
  xmlSerializer.setRootName("xml");
  //  Do not 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;
}

Related articles: