依赖
<dependency><groupId>com.fasterxml.jackson.dataformat</groupId><artifactId>jackson-dataformat-xml</artifactId></dependency>
代码
@PostMapping("parseQAJsonFile")@ApiOperation("解析json文件")public ResponseResult<Document> parseQAJsonFile(@RequestParam("file")MultipartFile file){if (file.isEmpty()) {return ResponseResult.error();}try {// 解析JSON文件ObjectMapper objectMapper = new ObjectMapper();Document document = objectMapper.readValue(new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8), Document.class);return ResponseResult.success(document);} catch (IOException e) {log.error("上传错误", e);return ResponseResult.error();}}
实体类
@JsonIgnoreProperties(ignoreUnknown = true) 可以忽略没有的字段,否则会报错。
@JsonProperty(“source_file_id”) 可以映射字段
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;import java.util.Map;@lombok.Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Document {@JsonProperty("source_file_id")private String sourceFileId;@JsonProperty("company_name")private String companyName;@JsonProperty("organization")private String organization;@JsonProperty("organization_code")private String organizationCode;@JsonProperty("approach")private String approach;@JsonProperty("source")private String source;@JsonProperty("label")private Map<String, String> label;@JsonProperty("first_class")private String firstClass;@JsonProperty("second_class")private String secondClass;@JsonProperty("third_class")private String thirdClass;@JsonProperty("fourth_class")private String fourthClass;@JsonProperty("fifth_class")private String fifthClass;@JsonProperty("sixth_class")private String sixthClass;@JsonProperty("title")private String title;@JsonProperty("author")private String author;@JsonProperty("summary")private String summary;@JsonProperty("pubdate_time")private String pubdateTime;@JsonProperty("effective_time")private String effectiveTime;@JsonProperty("expiry_time")private String expiryTime;@JsonProperty("language")private String language;@JsonProperty("security_level")private String securityLevel;@JsonProperty("clean_mode")private String cleanMode;@JsonProperty("applicable_scene")private String applicableScene;@JsonProperty("clean_rule_code")private String cleanRuleCode;@JsonProperty("cleaner")private String cleaner;@JsonProperty("clean_time")private String cleanTime;@JsonProperty("data_model_code")private String dataModelCode;@JsonProperty("data")private Data data;
}
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;import java.util.List;@lombok.Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Data {@JsonProperty("genaration_type")private String genarationType;@JsonProperty("task_type")private String taskType;@JsonProperty("total_round")private int totalRound;@JsonProperty("qa_list")private List<QA> qaList;
}
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class QA {@JsonProperty("question")private String question;@JsonProperty("answer")private String answer;@JsonProperty("ref_file")private String refFile;@JsonProperty("ref_text")private String refText;
}