Master Fastjson: Fast Java JSON Parsing and Serialization Guide
This article introduces JSON basics, explains the Fastjson library from Alibaba, outlines its key features, and provides practical Java code examples for parsing, serializing, and converting between JSON strings, objects, arrays, maps, lists, and beans using Fastjson’s API.
First we explain what JSON is: a lightweight data interchange format based on a subset of ECMAScript, independent of programming languages, with a clear hierarchical structure that is easy for humans and machines.
Fastjson is an open‑source JSON processor from Alibaba, written in Java, known for its speed.
Key Features
Conforms to the http://json.org standard and is listed as a reference implementation.
Supports all JDK types, including JavaBeans, Collections, Maps, Dates, Enums, and generics.
Zero external dependencies; runs directly on the JDK.
Open‑source under the Apache License 2.0.
The main entry class is com.alibaba.fastjson.JSON, whose static methods perform most serialization and deserialization tasks.
Core API Methods
public static final Object parse(String text);
public static final JSONObject parseObject(String text);
public static final <T> T parseObject(String text, Class<T> clazz);
public static final JSONArray parseArray(String text);
public static final <T> List<T> parseArray(String text, Class<T> clazz);
public static final String toJSONString(Object object);
public static final String toJSONString(Object object, boolean prettyFormat);
public static final Object toJSON(Object javaObject);Additional class notes: SerializeWriter works like a StringBuffer; JSONArray behaves like List<Object>; JSONObject behaves like Map<String, Object>.
Typical Usage Examples
Parse a JSON string into a JSONObject and read a value:
String str = "{\"name\":\"乱敲代码\"}";
JSONObject object = JSON.parseObject(str);
System.out.println(object.get("name"));Convert JSON to a Java bean (properties must match):
String s = "{\"id\":\"0375\",\"city\":\"上海\"}";
Weibo bean = JSON.parseObject(s, Weibo.class);
System.out.println(bean.getId());
System.out.println(bean.getCity());Serialize a List to JSON:
List<String> list = Lists.newArrayList();
list.add("slm");
list.add("乱敲代码");
String result = JSON.toJSONString(list);Serialize a Map to JSON:
Map<String, Object> map = Maps.newHashMap();
map.put("1", 123);
map.put("slm", "乱敲代码");
String result1 = JSON.toJSONString(map);Convert a Java bean to a JSONObject:
Bean bean = new Bean("1234", "洛阳");
JSONObject json = (JSONObject) JSON.toJSON(bean);
System.out.println(json.get("id"));Parse a JSON array string and access its elements:
String s = "[{\"id\":\"0375\",\"city\":\"平顶山\"},{\"id\":\"0377\",\"city\":\"南阳\"}]";
JSONArray array = JSON.parseArray(s);
String str = array.get(1) + "";
System.out.println(array.get(0));
JSONObject object = JSON.parseObject(str);
System.out.println(object.get("id"));Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
