fastjson中new TypeReference
时间: 2023-10-12 14:20:45 浏览: 49
在fastjson中,TypeReference是用来解决泛型类型擦除的问题的。由于Java中的泛型是在编译期间进行类型擦除的,因此在运行时无法获取泛型类型的具体信息。而TypeReference可以通过继承该类,并指定泛型类型,来获取泛型类型的具体信息,从而在序列化和反序列化时使用。例如:
```java
List<String> list = new ArrayList<String>();
list.add("hello");
TypeReference<List<String>> typeRef = new TypeReference<List<String>>(){};
String json = JSON.toJSONString(list, typeRef);
List<String> newList = JSON.parseObject(json, typeRef.getType());
```
在上述代码中,我们通过继承TypeReference类,并将泛型类型指定为List<String>,来获取泛型类型的具体信息。然后在序列化和反序列化时,通过getType()方法获取到具体的泛型类型,从而正确地进行转换。
相关问题
fastjson字符串转枚举 TypeReference
要将Fastjson字符串转换为枚举类型,可以使用TypeReference来实现。以下是一个示例代码:
```java
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
public class Main {
public static void main(String[] args) {
String jsonString = "\"ENUM_VALUE\"";
TypeReference<MyEnum> typeRef = new TypeReference<MyEnum>() {};
MyEnum myEnum = JSON.parseObject(jsonString, typeRef);
System.out.println(myEnum);
}
}
enum MyEnum {
ENUM_VALUE
}
```
在这个示例中,我们将Fastjson字符串`"ENUM_VALUE"`转换为`MyEnum`枚举类型。我们首先创建一个`TypeReference<MyEnum>`对象来表示要转换的目标类型。然后,使用`JSON.parseObject()`方法将字符串转换为枚举对象。最后,打印输出转换后的枚举值。
请注意,你需要添加Fastjson库的依赖才能运行此代码。
详细讲解一下fastjson中JSON parse方法
Fastjson 中的 `JSON.parse()` 方法用于将 JSON 字符串解析为 Java 对象或者数组。该方法有多个重载形式,可以根据传入的参数类型不同返回不同的对象类型。
以下是 `JSON.parse()` 方法的几种常用形式:
1. 返回 JSONObject 对象
```java
public static JSONObject parseObject(String text);
public static JSONObject parseObject(String text, Feature... features);
public static JSONObject parseObject(String text, ParserConfig config);
public static JSONObject parseObject(String text, TypeReference<?> type, Feature... features);
```
示例:
```java
String jsonStr = "{\"name\":\"Tom\",\"age\":18,\"gender\":\"male\"}";
JSONObject jsonObj = JSON.parseObject(jsonStr);
```
2. 返回 JSONArray 对象
```java
public static JSONArray parseArray(String text);
public static JSONArray parseArray(String text, Feature... features);
public static JSONArray parseArray(String text, ParserConfig config);
public static <T> List<T> parseArray(String text, Class<T> clazz, Feature... features);
public static <T> List<T> parseArray(String text, TypeReference<T> type, Feature... features);
```
示例:
```java
String jsonStr = "[\"apple\",\"banana\",\"orange\"]";
JSONArray jsonArray = JSON.parseArray(jsonStr);
```
3. 返回指定类型的 Java 对象
```java
public static <T> T parseObject(String text, Class<T> clazz);
public static <T> T parseObject(String text, TypeReference<T> type, Feature... features);
```
示例:
```java
String jsonStr = "{\"name\":\"Tom\",\"age\":18,\"gender\":\"male\"}";
User user = JSON.parseObject(jsonStr, User.class);
```
其中,`User` 是一个自定义的 Java 类型。
4. 返回任意类型的 Java 对象
```java
public static <T> T parseObject(String text, Type type, Feature... features);
```
示例:
```java
String jsonStr = "{\"name\":\"Tom\",\"age\":18,\"gender\":\"male\"}";
Type userType = new TypeReference<User>(){}.getType();
User user = JSON.parseObject(jsonStr, userType);
```
其中,`TypeReference<User>(){}.getType()` 用于获取 `User` 类型的反射对象。
以上就是 `JSON.parse()` 方法的常用形式和示例。在使用时,可以根据需要选择不同的重载形式,以便正确地解析 JSON 字符串并转换为指定的 Java 对象类型。