JSON
解析方式
Gson
Gson 是一个 Google 提供的一个 Java 库,可用于将 Java 对象转换为 JSON 表示。它也可以被用来
将 JSON 字符串转换成一个等效的 Java 对象。
json解析成java对象
@WebServlet("/js01")
public class JsonServlet01 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("=================对象转json=================");
User user = new User("余爽","宜春",true);
Gson gson = new Gson();
String json = gson.toJson(user);
System.out.println(json);
System.out.println("=================json转对象=================");
String json1 = "{\"name\":\"余爽\",\"address\":\"宜春\",\"sex\":true}";
Gson gson1 = new Gson();
User user1 = gson1.fromJson(json1, User.class);
System.out.println(user1);
}
}
Json 解析 成 List
@WebServlet("/js02")
public class JsonServlet02 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("=================List转json=================");
List<User> userList = new ArrayList<>();
userList.add(new User("余爽","宜春",true));
userList.add(new User("刘杭","萍乡",true));
userList.add(new User("万鲲","长沙",true));
Gson gson = new Gson();
String json = gson.toJson(userList);
System.out.println(json);
System.out.println("=================json转List=================");
String json1 = "[{\"name\":\"余爽\",\"address\":\"宜春\",\"sex\":true},{\"name\":\"刘杭\",\"address\":\"萍乡\",\"sex\":true},{\"name\":\"万鲲\",\"address\":\"长沙\",\"sex\":true}]";
Gson gson1 = new Gson();
List<User> userList1 = gson1.fromJson(json1, new TypeToken<List<User>>(){}.getType());
for (User user : userList1) {
System.out.println(user);
}
}
}
json解析成map
@WebServlet("/js03")
public class JsonServlet03 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("=================Map转json=================");
Map<String, Object> map = new HashMap<>();
map.put("id", "008");
map.put("user", new User("余爽","宜春",true));
Gson gson = new Gson();
String json = gson.toJson(map);
System.out.println(json);
System.out.println("=================json转Map=================");
String json1 = "{\"id\":\"008\",\"user\":{\"name\":\"罗碧娴\",\"address\":\"抚州\",\"sex\":false}}";
Gson gson1 = new Gson();
Map<String,Object> map1 = gson1.fromJson(json1, new TypeToken<Map<String,Object>>(){}.getType());
for (String key : map1.keySet()) {
System.out.println(key + ":" + map1.get(key));
}
}
}
#### Json 解析 成 List
```java
@WebServlet("/js02")
public class JsonServlet02 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("=================List转json=================");
List<User> userList = new ArrayList<>();
userList.add(new User("余爽","宜春",true));
userList.add(new User("刘杭","萍乡",true));
userList.add(new User("万鲲","长沙",true));
Gson gson = new Gson();
String json = gson.toJson(userList);
System.out.println(json);
System.out.println("=================json转List=================");
String json1 = "[{\"name\":\"余爽\",\"address\":\"宜春\",\"sex\":true},{\"name\":\"刘杭\",\"address\":\"萍乡\",\"sex\":true},{\"name\":\"万鲲\",\"address\":\"长沙\",\"sex\":true}]";
Gson gson1 = new Gson();
List<User> userList1 = gson1.fromJson(json1, new TypeToken<List<User>>(){}.getType());
for (User user : userList1) {
System.out.println(user);
}
}
}
json解析成map
@WebServlet("/js03")
public class JsonServlet03 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("=================Map转json=================");
Map<String, Object> map = new HashMap<>();
map.put("id", "008");
map.put("user", new User("余爽","宜春",true));
Gson gson = new Gson();
String json = gson.toJson(map);
System.out.println(json);
System.out.println("=================json转Map=================");
String json1 = "{\"id\":\"008\",\"user\":{\"name\":\"罗碧娴\",\"address\":\"抚州\",\"sex\":false}}";
Gson gson1 = new Gson();
Map<String,Object> map1 = gson1.fromJson(json1, new TypeToken<Map<String,Object>>(){}.getType());
for (String key : map1.keySet()) {
System.out.println(key + ":" + map1.get(key));
}
}
}