com.alibaba.fastjson public static <T> List<T> parseArray(String text, Class<T> clazz)
时间: 2023-05-21 17:03:55 浏览: 257
这是一个用于将 JSON 字符串转换为 Java 对象列表的方法。其中,text 是 JSON 字符串,clazz 是要转换成的 Java 对象类型。该方法使用了泛型,可以适用于不同类型的 Java 对象。
相关问题
com.alibaba.fastjson public static <T> List<T> parseArray(String text, Class<T> clazz)中的text如何接收json字符串
在com.alibaba.fastjson public static <T> List<T> parseArray(String text, Class<T> clazz)中,text参数是一个String类型的参数,用于接收JSON格式的字符串。这个方法会将JSON字符串解析成一个List<T>类型的对象,其中T是clazz指定的类型。
fastjson String 转 JsonData 和JsonArray示例
<think>我们被要求提供使用FastJson库将字符串转换为JsonObject(即JsonData)和JsonArray的示例代码。
根据引用[2]中的内容,FastJson提供了以下方法:
- 将字符串解析为JSONObject:JSON.parseObject(String text)
- 将字符串解析为JSONArray:JSON.parseArray(String text)
同时,如果要将字符串解析为特定类型的JavaBean,可以使用:
- JSON.parseObject(String text, Class<T> clazz)
- 解析为特定类型的List:JSON.parseArray(String text, Class<T> clazz)
但用户的问题中并没有提到具体的JavaBean类型,所以我们将提供基本的使用示例。
示例步骤:
1. 添加FastJson依赖(如果项目中尚未添加)
2. 导入必要的类
3. 示例代码:字符串转JsonObject(即JSONObject)
4. 示例代码:字符串转JsonArray(即JSONArray)
注意:JsonData在FastJson中通常指的是JSONObject(表示一个JSON对象)或JSONArray(表示一个JSON数组),但有时也可能指具体的Java对象。根据上下文,这里我们理解为JSONObject和JSONArray。
假设我们有以下两个字符串:
- 表示对象的字符串:`String jsonObjectStr = "{\"name\":\"John\", \"age\":30}";`
- 表示数组的字符串:`String jsonArrayStr = "[{\"name\":\"John\", \"age\":30}, {\"name\":\"Jane\", \"age\":25}]";`
我们将使用FastJson的API进行转换。</think>### FastJSON 字符串转 JSONObject 和 JSONArray 示例
以下是使用 FastJSON 库将字符串转换为 JSONObject 和 JSONArray 的完整示例代码:
```java
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class FastJsonExample {
public static void main(String[] args) {
// 示例1: 字符串转 JSONObject (JsonData)
String jsonStr = "{\"name\":\"John\", \"age\":30, \"isStudent\":false}";
// 方法1: 直接解析为 JSONObject
JSONObject jsonObject1 = JSON.parseObject(jsonStr);
System.out.println("JSONObject 解析结果:");
System.out.println("姓名: " + jsonObject1.getString("name"));
System.out.println("年龄: " + jsonObject1.getInteger("age"));
System.out.println("是否学生: " + jsonObject1.getBoolean("isStudent"));
// 方法2: 先解析为通用对象再转换
Object obj = JSON.parse(jsonStr);
if (obj instanceof JSONObject) {
JSONObject jsonObject2 = (JSONObject) obj;
System.out.println("\n转换结果验证: " + jsonObject2.toJSONString());
}
// 示例2: 字符串转 JSONArray
String arrayStr = "[{\"id\":1,\"product\":\"Laptop\"}, {\"id\":2,\"product\":\"Phone\"}]";
// 方法1: 直接解析为 JSONArray
JSONArray jsonArray1 = JSON.parseArray(arrayStr);
System.out.println("\nJSONArray 解析结果:");
for (int i = 0; i < jsonArray1.size(); i++) {
JSONObject item = jsonArray1.getJSONObject(i);
System.out.println("商品" + (i+1) + ": ID=" + item.getInteger("id")
+ ", 名称=" + item.getString("product"));
}
// 方法2: 使用泛型指定元素类型
List<Product> products = JSON.parseArray(arrayStr, Product.class);
System.out.println("\n转换为Java对象:");
products.forEach(p -> System.out.println(p));
}
// 辅助类
static class Product {
private int id;
private String product;
// Getters/Setters
@Override
public String toString() {
return "Product{id=" + id + ", product='" + product + "'}";
}
}
}
```
#### 关键方法说明:
1. **字符串转 JSONObject**:
```java
// 直接解析
JSONObject obj = JSON.parseObject(jsonString);
// 解析为特定类型
MyClass obj = JSON.parseObject(jsonString, MyClass.class);
```
2. **字符串转 JSONArray**:
```java
// 直接解析
JSONArray array = JSON.parseArray(jsonArrayString);
// 解析为特定类型列表
List<MyClass> list = JSON.parseArray(jsonArrayString, MyClass.class);
```
3. **类型转换方法**:
```java
jsonObject.getString("key") // 获取字符串值
jsonObject.getInteger("key") // 获取整数值
jsonObject.getBoolean("key") // 获取布尔值
jsonArray.getJSONObject(index) // 获取数组元素
```
#### 输出结果示例:
```
JSONObject 解析结果:
姓名: John
年龄: 30
是否学生: false
转换结果验证: {"isStudent":false,"name":"John","age":30}
JSONArray 解析结果:
商品1: ID=1, 名称=Laptop
商品2: ID=2, 名称=Phone
转换为Java对象:
Product{id=1, product='Laptop'}
Product{id=2, product='Phone'}
```
#### 依赖配置(Maven):
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version> <!-- 使用最新版本 -->
</dependency>
```
> **注意**:实际使用时需处理可能的异常 `JSONException`,确保输入字符串是有效的 JSON 格式[^2]。
---
### 相关问题
1. FastJSON 如何处理嵌套的 JSON 对象和数组?
2. 使用 FastJSON 时有哪些常见的安全风险及防范措施?
3. FastJSON 与其他 JSON 库(如 Gson、Jackson)的主要区别是什么?
4. 如何将 JSONObject 或 JSONArray 转换回 JSON 字符串?
5. FastJSON 是否支持自定义序列化和反序列化规则?
阅读全文
相关推荐














