com.alibaba
fastjson
2.0.1
在fastjson 2.0中,package和1.x不一样,是com.alibaba.fastjson2。如果你之前用的是fastjson1,大多数情况直接更包名就即可。
package com.alibaba.fastjson2;
class JSON {
// 将字符串解析成JSONObject
static JSONObject parseObject(String str);
// 将字符串解析成JSONArray
static JSONArray parseArray(String str);
// 将字符串解析成Java对象
static T parseObject(byte[] utf8Bytes, Class objectClass);
// 将Java对象输出成字符串
static String toJSONString(Object object);
// 将Java对象输出成UT8编码的byte[]
static byte[] toJSONBytes(Object object);
}
class JSONB {
// 将jsonb格式的byte[]解析成Java对象
static T parseObject(byte[] jsonbBytes, Class objectClass);
// 将Java对象输出成jsonb格式的byte[]
static byte[] toBytes(Object object);
}
class JSONObject {
Object get(String key);
int getIntValue(String key);
Integer getInteger(String key);
long getLongValue(String key);
Long getLong(String key);
T getObject(String key, Class objectClass);
// 将JSONObject对象转换为Java对象
T toJavaObject(Class objectClass);
}
class JSONArray {
Object get(int index);
int getIntValue(int index);
Integer getInteger(int index);
long getLongValue(int index);
Long getLong(int index);
T getObject(int index, Class objectClass);
}
class JSONPath {
// 构造JSONPath
static JSONPath of(String path);
// 根据path直接解析输入,会部分解析优化,不会全部解析
Object extract(JSONReader jsonReader);
// 根据path对对象求值
Object eval(Object rootObject);
}
class JSONReader {
// 构造基于String输入的JSONReader
static JSONReader of(String str);
// 构造基于ut8编码byte数组输入的JSONReader
static JSONReader of(byte[] utf8Bytes);
// 构造基于char[]输入的JSONReader
static JSONReader of(char[] chars);
// 构造基于json格式byte数组输入的JSONReader
static JSONReader ofJSONB(byte[] jsonbBytes)
}
String str = "{\"id\":123}";
JSONObject jsonObject = JSON.parseObject(str);
int id = jsonObject.getIntValue("id");
String str = "[\"id\", 123]";
JSONArray jsonArray = JSON.parseArray(str);
String name = jsonArray.getString(0);
int id = jsonArray.getIntValue(1);
class Product {
public int id;
public String name;
}
Product product = new Product();
product.id = 1001;
product.name = "DataWorks";
JSON.toJSONString(product);
// 生成如下的结果
{
"id" : 1001,
"name" : "DataWorks"
}
JSON.toJSONString(product, JSONWriter.Feature.BeanToArray);
// 生成如下的结果
[123, "DataWorks"]
Product product = ...;
byte[] utf8JSONBytes = JSON.toJSONBytes(product);
Product product = ...;
byte[] jsonbBytes = JSONB.toBytes(product);
byte[] jsonbBytes = JSONB.toBytes(product, JSONWriter.Feature.BeanToArray);
String str = "{\"id\":123}";
Product product = JSON.parseObject(str, Product.class);
byte[] jsonbBytes = ...
Product product = JSONB.parseObject(jsonbBytes, Product.class);
Product product = JSONB.parseObject(jsonbBytes, Product.class, JSONReader.Feature.SupportBeanArrayMapping);
String str = ...;
JSONPath path = JSONPath.of("$.id"); // 缓存起来重复使用能提升性能
JSONReader parser = JSONReader.of(str);
Object result = path.extract(parser);
.
byte[] utf8Bytes = ...;
JSONPath path = JSONPath.of("$.id"); // 缓存起来重复使用能提升性能
JSONReader parser = JSONReader.of(utf8Bytes);
Object result = path.extract(parser);
byte[] jsonbBytes = ...;
JSONPath path = JSONPath.of("$.id"); // 缓存起来重复使用能提升性能
JSONReader parser = JSONReader.ofJSONB(jsonbBytes); // 注意,这是利用ofJSONB方法
Object result = path.extract(parser);
详情可查看:https://github.com/alibaba/fastjson2/releases