"param":{"workTypes":["TRANSFER","BEFORE_APPEND","AFTER_APPEND","SELF","OPINION","AGENT","ORIGINATOR_SUBMIT"]}这段json数据怎么转换成一个java的String字符串
时间: 2023-07-21 22:05:51 浏览: 58
要将这段JSON数据转换为Java的字符串,你可以使用Java的JSON库,如Jackson或Gson。以下是使用Jackson库的示例代码:
```java
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
// JSON数据
String json = "{\"param\":{\"workTypes\":[\"TRANSFER\",\"BEFORE_APPEND\",\"AFTER_APPEND\",\"SELF\",\"OPINION\",\"AGENT\",\"ORIGINATOR_SUBMIT\"]}}";
try {
// 将JSON数据转换为Java对象
Object obj = objectMapper.readValue(json, Object.class);
// 将Java对象转换为字符串
String jsonString = objectMapper.writeValueAsString(obj);
// 输出结果
System.out.println(jsonString);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
运行以上代码,你将得到将JSON数据转换为Java字符串的结果。请注意,你需要在项目中添加Jackson库的依赖。
阅读全文