更新时间:2023-01-17 16:04:36
这是一个适合我的杰克逊例子。我在单个json文件中有数千个json对象(令牌)。此代码将遍历文件读取每个令牌并打印它的序列。
Here is a Jackson example that works for me. I have thousands json objects (tokens) in a single json file. This code will iterate through the file read each token and print it's serial.
必需的导入:
Required imports:
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
使用Jackson从 FileInputStream
中读取多个json对象>:
Using Jackson to read multiple json objects from FileInputStream
:
try (FileInputStream fis = new FileInputStream("D:/temp/tokens.json")) {
JsonFactory jf = new JsonFactory();
JsonParser jp = jf.createParser(fis);
jp.setCodec(new ObjectMapper());
jp.nextToken();
while (jp.hasCurrentToken()) {
Token token = jp.readValueAs(Token.class);
jp.nextToken();
System.out.println("Token serial "+token.getSerialNumber());
}
}