java实现json对象转string[]
时间: 2023-09-18 19:06:36 浏览: 49
可以使用Java中的Jackson库来实现将JSON对象转换为String数组。以下是一个示例代码:
```java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonToStringArray {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
String jsonStr = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
// 将JSON字符串转换为JSON对象
Object obj = objectMapper.readValue(jsonStr, Object.class);
// 将JSON对象转换为String数组
String[] strArr = objectMapper.writeValueAsString(obj).split(",");
// 打印输出String数组
for (String s : strArr) {
System.out.println(s);
}
}
}
```
在上面的代码中,我们使用Jackson库中的ObjectMapper对象来将JSON字符串转换为JSON对象,并使用该对象将JSON对象转换为String数组。最后,我们遍历该数组并打印输出。