读取JSON文件中指定部分数据(小文件)
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONObject;
public class JsonSourceExtractor {
public static void main(String[] args) {
String jsonDataStr = "{\"some_key1\": \"some_value1\", \"_source\": {\"desired_key1\": \"desired_value1\"}}{\"some_key2\": \"some_value2\", \"_source\": {\"desired_key2\": \"desired_value2\"}}";
List<JSONObject> sources = extractSources(jsonDataStr);
for (JSONObject source : sources) {
System.out.println(source);
}
}
public static List<JSONObject> extractSources(String jsonDataStr) {
List<JSONObject> result = new ArrayList<>();
Pattern pattern = Pattern.compile("_source\":\\{.*?\\}");
Matcher matcher = pattern.matcher(jsonDataStr);
while (matcher.find()) {
String sourceStr = matcher.group();
sourceStr = sourceStr.replace("_source\":", "");
try {
JSONObject source = new JSONObject(sourceStr);
result.add(source);
} catch (Exception e) {
System.out.println("无法解析的部分:" + sourceStr);
}
}
return result;
}
}
读取JSON文件中指定部分数据(大文件)
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson - core</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson - databind</artifactId>
<version>2.13.0</version>
</dependency>
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class BigJsonJacksonSourceExtractor {
public static void main(String[] args) {
JsonFactory factory = new JsonFactory();
ObjectMapper mapper = new ObjectMapper(factory);
List<JsonNode> sources = new ArrayList<>();
try {
JsonParser parser = factory.createParser(new File("your_large_file.json"));
while (!parser.isClosed()) {
JsonNode node = mapper.readTree(parser);
if (node.has("_source")) {
sources.add(node.get("_source"));
}
}
parser.close();
for (JsonNode source : sources) {
System.out.println(source);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
读取JSON文件中指定部分数据(按行)
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONObject;
public class BigJsonSourceExtractor {
public static void main(String[] args) {
try {
List<JSONObject> sources = new ArrayList<>();
BufferedReader reader = new BufferedReader(new FileReader("your_large_file.json"));
String line;
Pattern pattern = Pattern.compile("_source\":\\{.*?\\}");
while ((line = reader.readLine())!= null) {
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
String sourceStr = matcher.group();
sourceStr = sourceStr.replace("_source\":", "");
try {
JSONObject source = new JSONObject(sourceStr);
sources.add(source);
} catch (Exception e) {
System.out.println("无法解析的部分:" + sourceStr);
}
}
}
reader.close();
for (JSONObject source : sources) {
System.out.println(source);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}