Java JSON递归遍历
我的目标是读取非结构化JSON文件并将该文件加载到数据库中: 基本上我有下表:Java JSON递归遍历,java,json,jackson,Java,Json,Jackson,我的目标是读取非结构化JSON文件并将该文件加载到数据库中: 基本上我有下表: drop table stg_json_structure; create table stg_json_structure ( id bigint not null primary key auto_increment, meta_data varchar(500), meta_value longtext, parent_id bigint, t
drop table stg_json_structure;
create table stg_json_structure
(
id bigint not null primary key auto_increment,
meta_data varchar(500),
meta_value longtext,
parent_id bigint,
type varchar(100),
array_index int,
created datetime,
foreign key (parent_id) references stg_json_structure(id)
)
其中:
id - PK
meta_data - Json key
meta_data - Json value
array_index - if it's Array type when put here array index
created - timestamp of inserted record
所以parent_id应该指向json文件中父密钥的id
我编写以下Java代码来读取Json文件递归
// 1
public static void undefinedJson() {
ObjectMapper mapper = new ObjectMapper();
try {
JsonParser parser = mapper.getJsonFactory().createJsonParser(new File("/tmp/test_json.txt"));
while (parser.nextToken() != null) {
System.out.println(parser.getCurrentToken());
if (JsonToken.START_ARRAY.equals(parser.getCurrentToken())) {
JsonNode node = parser.readValueAsTree();
readJsonArrayNode(node);
}
else if (JsonToken.START_OBJECT.equals(parser.getCurrentToken())) {
JsonNode node = parser.readValueAsTree();
readJsonObjectNode(node);
}
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
// 2
public static void readJsonArrayNode(JsonNode node) {
if (node.isArray()) {
for (int i=0;i<node.size();i++) {
System.out.println("array index="+i);
JsonNode child = node.get(i);
if (child.isObject()) {
readJsonObjectNode(child);
}
else if (child.isArray()) {
readJsonArrayNode(child);
}
else {
System.out.println(child);
}
}
}
}
// 3
public static void readJsonObjectNode(JsonNode node) {
if (node.isObject()) {
Iterator<Map.Entry<String,JsonNode>> i=node.getFields();
while (i.hasNext()) {
Map.Entry<String,JsonNode> jsonNode=i.next();
String key = jsonNode.getKey();
JsonNode temp = jsonNode.getValue();
if (temp.isArray()) {
System.out.print(key+"=");
System.out.println(temp);
readJsonArrayNode(temp);
}
else if (temp.isObject()) {
System.out.println();
System.out.println("ObjectNode");
System.out.print(key+"=");
System.out.println(temp);
readJsonObjectNode(temp);
}
else {
if (temp.isValueNode()) {
System.out.println();
System.out.println("ValueNode");
System.out.print(key+"=");
System.out.println(temp);
}
}
}
}
}
//4
public static void main(String[] args) {
undefinedJson();
}
//1
公共静态void undefinedJson(){
ObjectMapper mapper=新的ObjectMapper();
试一试{
JsonParser parser=mapper.getJsonFactory().createJsonParser(新文件(“/tmp/test_json.txt”);
while(parser.nextToken()!=null){
System.out.println(parser.getCurrentToken());
if(JsonToken.START_ARRAY.equals(parser.getCurrentToken())){
JsonNode=parser.readValueAsTree();
readJsonArrayNode(节点);
}
else if(JsonToken.START_OBJECT.equals(parser.getCurrentToken())){
JsonNode=parser.readValueAsTree();
readJsonObjectNode(节点);
}
}
}
捕获(例外情况除外){
例如printStackTrace();
}
}
// 2
公共静态void readJsonArrayNode(JsonNode节点){
if(node.isArray()){
对于(int i=0;i您知道JSON值也可以是基元类型,对吗?是的,您的意思是需要为此添加额外的签入readJsonObjectNode?基本上,这对我来说并不重要…我需要知道如何处理父项id和当前项id/子项id,知道如何保留每个JSON键的id吗?