introduce
JSONUtil is a static shortcut method collection for JSONObject and JSONArray. In the previous chapter, we have introduced some tool methods, and we will make some additions in this chapter.
use
1. JSON string creation
You can directly convert any object (Bean, Map, Collection, etc.) into JSON strings. If the object is an ordered Map and other objects, the converted JSON string is also ordered.
/** * Convert to JSON string * <p> * Object converted to JSON * * @return JSON string */ @Test void toJsonStrTest() { //map SortedMap<Object, Object> sortedMap = new TreeMap<Object, Object>() { private static final long serialVersionUID = 1L; { put("attributes", "a"); put("b", "b"); put("c", "c"); } }; //Object Student beanString = new Student(1, 1, 1, "Zhang San"); //gather List<Student> list = new ArrayList<>(); (new Student(1, 1, 1, "Zhang San")); (new Student(1, 1, 2, "Li Si")); (new Student(1, 1, 3, "Wang Wu")); ("(beanString) = " + (beanString)); ("(sortedMap) = " + (sortedMap)); ("(list) = " + (list)); }
result
(beanString) = {"gradeId":0,"studentId":1,"termId":1,"classId":1,"name":"Zhang San"}
(sortedMap) = {"attributes":"a","b":"b","c":"c"}
(list) = [{"gradeId":0,"studentId":1,"termId":1,"classId":1,"name":"Zhang San"},
{"gradeId":0,"studentId":2,"termId":1,"classId":1,"name":"Li Si"},
{"gradeId":0,"studentId":3,"termId":1,"classId":1,"name":"Wang Wu"}]
If we want to get formatted JSON, then:
(object); ("(sortedMap) = " + (sortedMap));
result
(sortedMap) = {
"attributes": "a",
"b": "b",
"c": "c"
}
2. JSON string parsing
/** * JSON string to JSONObject object * <p> * JSON string * * @return JSONObject */ @Test void Test() { String html = "{\"name\":\"Something must have been changed since you leave\"}"; JSONObject jsonObject = (html); ("(\"name\") = " + ("name")); }
result
("name") = Something must have been changed since you leave
3. Convert XML string to JSON
/** * XML string to JSONObject * * XML string * @return JSONObject */ @Test void parseFromXmlTest() { String s = "<sfzh>123</sfzh><sfz>456</sfz><name>aa</name><gender>1</gender>"; JSONObject json = (s); ("(\"sfzh\") = " + ("sfzh")); ("(\"name\") = " + ("name")); }
result
("sfzh") = 123
("name") = aa
4. Convert JSON to XML
/** * Convert to XML string * * @param json JSON * @return XML string */ @Test void toXmlStrTest() { final JSONObject put = () .set("aaa", "Hello") .set("Key 2", "test"); // <aaa>Hello</aaa><key 2>test</key 2> final String s = (put); ("s = " + s); }
result
s = <aaa>Hello</aaa><key 2>test</key 2>
5. JSON to Bean
Let's first define two more complex beans (including generics)
@Data public class ADT { private List<String> BookingCode; } @Data public class Price { private List<List<ADT>> ADT; } /** * JSON string is converted into entity class object, conversion exception will be thrown * * @param <T> Bean type * @param jsonString JSON string * @param beanClass Entity class object * @return Entity class object * @since 3.1.2 */ @Test void toBeanTest() { String json = "{\"ADT\":[[{\"BookingCode\":[\"N\",\"N\"]}]]}"; Price price = (json, ); ("price = " + price); }
The following are common methods for JSON in Hutool:
Parse and generation of JSON strings:
-
(jsonStr)
: Parses the JSON string into a JSONObject object. -
(jsonStr)
: Parses the JSON string into a JSONArray array object. -
(obj)
: Convert Java objects to JSON strings.
Conversion of objects to JSON strings:
-
(jsonStr, clazz)
: Converts a JSON string to a Java object of the specified type. -
(jsonStr)
: Converts a JSON string to a Map object. -
(bean)
: Convert Java objects to camel-style Map objects, suitable for most JavaBeans.
Operation of JSON object:
-
(key)
: Gets the value of the specified key in the JSON object. -
(key, value)
: Add key-value pairs to the JSON object. -
(key)
: Determines whether the specified key is included in the JSON object. -
()
: Gets a collection of all keys in a JSON object.
Operation of JSON array:
-
(index)
: Gets the element of the specified index in the JSON array. -
(value)
: Add elements to the JSON array. -
()
: Get the length of the JSON array. -
()
: Gets the iterator of the JSON array.
Other common methods:
-
(xmlStr)
: Parses an XML string into a JSON object. -
(jsonObject)
: Convert a JSON object to an XML string.
Summarize
This is the article about how to use java hutool tool class to process JSON. For more related java hutool JSON content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!