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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值