欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > java解析json

java解析json

2024/12/2 17:03:35 来源:https://blog.csdn.net/qq_37398465/article/details/144099216  浏览:    关键词:java解析json

依赖

	<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;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com