大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
本文使用alibaba开源项目fastjson与SourceForge开源项目JSON-lib两种方法转换json。
现有json文件user.json如下:
[
{
"userid": "309BCCE3-8525-4D33-9856-5084E24FD96F",
"username": "林梦",
"password": "19bdec7440acd44c669240ed534fc2f6",
"partment": "专案一组",
"tel": "123",
"email": "meng@baal.xyz",
"tag": "专案",
"creater": "",
"time": "2016-10-24T08:10:51.101Z",
"token": ""
},
{
"userid": "FDA8B18B-E6EB-4C80-8A4B-0B2B27AEB7E2",
"username": "汪小雨",
"password": "19bdec7440acd44c669240ed534fc2f6",
"partment": "专案三组",
"tel": "123",
"email": "xiaoyu@baal.xyz",
"tag": "专案",
"creater": "",
"time": "2016-10-24T08:12:28.869Z",
"token": ""
}
]
创建数据模型User类,属性如下:
public class User {
private String userid;
private String username;
private String password;
private String partment;
private String tel;
private String email;
private String tag;
private String creater;
private String time;
private String token;
......
}
1、使用fastjson
读取json文件转化为list:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class JsonToList_fastjson {
private static final String User_Path = "d:/user.json";
private static final String Message_Path = "d:/message.json";
public static List<User> getUsers() {
String userString = fileToStr(User_Path);
List<User> list = JSONObject.parseArray(userString,User.class);
return list;
}
public static String fileToStr(String filepath) {
File file = new File(filepath);
StringBuffer sb = new StringBuffer();
InputStreamReader isr = null;
try {
isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
while (isr.ready()) {
sb.append((char) isr.read());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public static void main (String [] args) {
List<User> listUsers = getUsers();
for (User user : listUsers) {
System.out.println(user.getEmail());
}
JSONArray array = (JSONArray) JSONArray.toJSON(listUsers);
System.out.println(array);
}
}
2、使用JSON-lib,相关jar包也可以在这里下载
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import net.sf.json.JSONArray;
public class JsonToList {
private static final String User_Path = "d:/user.json";
@SuppressWarnings("unchecked")
public static List<User> getUsers() {
String userString = fileToStr(User_Path);
JSONArray array = JSONArray.fromObject(userString);
List<User> list = null;
list = (List<User>) JSONArray.toCollection(array, User.class);
return list;
}
public static String fileToStr(String filepath) {
File file = new File(filepath);
StringBuffer sb = new StringBuffer();
InputStreamReader isr = null;
try {
isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
while (isr.ready()) {
sb.append((char) isr.read());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public static void main(String[] args) {
List<User> list = getUsers();
for (User user : list) {
System.out.println(user.getUsername());
}
JSONArray array = JSONArray.fromObject(list);
System.out.println(array);
}
}
JSON-lib API :Doc
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/172289.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...