package com.xxx;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

public class TestJsonNode {

    @Test
    public void test()
    {
        String json = "{\"status\": 0, \"segment\": 0, \"result\": {\"hypotheses\": [{\"transcript\": \"原 的 \"}], \"final\": false}, \"id\": \"ba040ef0-9134-46f3-8a95-cc9b70868c07\"}";
        ObjectMapper objectMapper = new ObjectMapper();
        try {
//            JsonNode node = objectMapper.readTree(json);
//            boolean containsStatusField = node.has("status");
//            if(!containsStatusField)
//            {
//                System.out.println("服务器异常");
//            }
//
//            int status = node.findValue("status").intValue();
            //System.out.println(status);

            ASRConversionResponse response = objectMapper.readValue(json, ASRConversionResponse.class);

            System.out.println(response);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

@Data
@NoArgsConstructor
public class ASRConversionResponse
{
    private Integer status;
    private String message;

    private List<Result> result;

    @JsonProperty("segment_start") //此处为自定义字段绑定类型
    private Float segmentStart;

    @JsonProperty("segment_length")
    private Float segmentLength;

    @JsonProperty("total_length")
    private Float totalLength;

    private short segment;

    private String id;

}

@Data
@NoArgsConstructor
  class Result{

    @JsonProperty("final")
    private Boolean _final;

    private List<Hypotheses> hypotheses;
}

@Data
@NoArgsConstructor
  class Hypotheses
{
    private Float likelihood;
    private String transcript;
}

}

出错原因:把ASRConversionResponse内部类提出来,单独新建一个类ASRConversionResponse.java

posted on 2019-08-07 17:17  你不知道的浪漫  阅读(8012)  评论(0编辑  收藏  举报