java将数据转化为json,在java中将各种类型数据转化为json跟从json中取数据
时间: 2023-08-14 14:06:36 浏览: 96
Java中可以使用各种JSON库将各种类型的数据转化为JSON格式,比如Gson、Jackson、FastJSON等等。以下是使用Gson和Jackson库将各种类型的数据转化为JSON格式并从JSON中取数据的示例:
1. 将Java对象转化为JSON字符串
使用Gson库:
```java
import com.google.gson.Gson;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MyClass {
public static void main(String[] args) {
Gson gson = new Gson();
// 将Java对象转化为JSON字符串
Person person = new Person("John", 30);
String json1 = gson.toJson(person);
System.out.println(json1);
// 将List转化为JSON字符串
List<String> list = Arrays.asList("apple", "banana", "orange");
String json2 = gson.toJson(list);
System.out.println(json2);
// 将Map转化为JSON字符串
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);
String json3 = gson.toJson(map);
System.out.println(json3);
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
```
使用Jackson库:
```java
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MyClass {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// 将Java对象转化为JSON字符串
Person person = new Person("John", 30);
String json1 = mapper.writeValueAsString(person);
System.out.println(json1);
// 将List转化为JSON字符串
List<String> list = Arrays.asList("apple", "banana", "orange");
String json2 = mapper.writeValueAsString(list);
System.out.println(json2);
// 将Map转化为JSON字符串
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);
String json3 = mapper.writeValueAsString(map);
System.out.println(json3);
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
```
2. 从JSON字符串中取数据
使用Gson库:
```java
import com.google.gson.Gson;
import java.util.List;
import java.util.Map;
public class MyClass {
public static void main(String[] args) {
Gson gson = new Gson();
// 从JSON字符串中取得Java对象
String json1 = "{\"name\":\"John\",\"age\":30}";
Person person = gson.fromJson(json1, Person.class);
System.out.println(person.getName());
System.out.println(person.getAge());
// 从JSON字符串中取得List
String json2 = "[\"apple\",\"banana\",\"orange\"]";
List<String> list = gson.fromJson(json2, List.class);
System.out.println(list);
// 从JSON字符串中取得Map
String json3 = "{\"apple\":1,\"banana\":2,\"orange\":3}";
Map<String, Integer> map = gson.fromJson(json3, Map.class);
System.out.println(map);
}
}
class Person {
private String name;
private int age;
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
```
使用Jackson库:
```java
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import java.util.Map;
public class MyClass {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// 从JSON字符串中取得Java对象
String json1 = "{\"name\":\"John\",\"age\":30}";
Person person = mapper.readValue(json1, Person.class);
System.out.println(person.getName());
System.out.println(person.getAge());
// 从JSON字符串中取得List
String json2 = "[\"apple\",\"banana\",\"orange\"]";
List<String> list = mapper.readValue(json2, List.class);
System.out.println(list);
// 从JSON字符串中取得Map
String json3 = "{\"apple\":1,\"banana\":2,\"orange\":3}";
Map<String, Integer> map = mapper.readValue(json3, Map.class);
System.out.println(map);
}
}
class Person {
private String name;
private int age;
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
```
以上就是使用Gson和Jackson库将各种类型数据转化为JSON格式并从JSON中取数据的示例。
阅读全文