json转换成实体类

json转换成实体类今天同事问我个问题。她需要把字符串的json转换成实体类,但是一直报错。主要是实体类当中的内部类需要用静态字段static修饰,否则无法访问。以下是报错信息org.codehaus.jackson.map.JsonMappingException:Nosuitableconstructorfoundfortype[simpletype,classcom.macxen.sp…

json转换成实体类"

今天同事问我个问题。她需要把字符串的json转换成实体类,但是一直报错。主要是实体类当中的内部类需要用静态字段static修饰,否则无法访问。
以下是报错信息

org.codehaus.jackson.map.JsonMappingException: 
No suitable constructor found for type [simple type, class com.macxen.speechrecognition.JsonRootBean$Result]
: can not instantiate from JSON object (need to add/enable type information?)
 at [Source: java.io.StringReader@3db5162; line: 1, column: 100] 
 (through reference chain: com.macxen.speechrecognition.JsonRootBean["origin_result"]
 ->com.macxen.speechrecognition.Origin_result["result"])

1.首先导入marven

<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-lgpl</artifactId>
			<version>1.9.13</version>
			<scope>test</scope>
</dependency>

2.jackonMapper

package com.sunian.besjon;

import org.codehaus.jackson.map.ObjectMapper;

/**
 * @author sunian
 * @name speechRecognition
 * @class name:com.sunian.speechrecognition.util
 * @time 2018/11/7 10:14
 * @describe describe
 */
public class JacksonMapper {
    private static final ObjectMapper mapper = new ObjectMapper();

    private JacksonMapper() {

    }

    public static ObjectMapper getInstance() {
        return mapper;
    }

}

3.JsonRootBean

package com.macxen.besjon;

import java.io.Serializable;
import java.util.List;

/**
 * @author lainuo
 * @name speechRecognition
 * @class name:com.macxen.speechrecognition
 * @time 2018/11/6 19:30
 * @describe 实体类
 */
public class JsonRootBean implements Serializable {


    private String result_type;
    private String best_result;
    private List<String> results_recognition;
    private Origin_result origin_result;
    private int error;

    public JsonRootBean() {
    }

    public void setResult_type(String result_type) {
        this.result_type = result_type;
    }

    public String getResult_type() {
        return result_type;
    }

    public void setBest_result(String best_result) {
        this.best_result = best_result;
    }

    public String getBest_result() {
        return best_result;
    }

    public void setResults_recognition(List<String> results_recognition) {
        this.results_recognition = results_recognition;
    }

    public List<String> getResults_recognition() {
        return results_recognition;
    }

    public void setOrigin_result(Origin_result origin_result) {
        this.origin_result = origin_result;
    }

    public Origin_result getOrigin_result() {
        return origin_result;
    }

    public void setError(int error) {
        this.error = error;
    }

    public int getError() {
        return error;
    }

    public static class Result implements Serializable {

        private List<String> word;

        public Result() {
        }

        public void setWord(List<String> word) {
            this.word = word;
        }

        public List<String> getWord() {
            return word;
        }

    }

    public static class Origin_result implements Serializable {

        private Result result;
        private String sn;
        private int err_no;
        private long corpus_no;
        private double voice_energy;

        public Origin_result() {
        }

        public void setResult(Result result) {
            this.result = result;
        }

        public Result getResult() {
            return result;
        }

        public void setSn(String sn) {
            this.sn = sn;
        }

        public String getSn() {
            return sn;
        }

        public void setErr_no(int err_no) {
            this.err_no = err_no;
        }

        public int getErr_no() {
            return err_no;
        }

        public void setCorpus_no(long corpus_no) {
            this.corpus_no = corpus_no;
        }

        public long getCorpus_no() {
            return corpus_no;
        }

        public void setVoice_energy(double voice_energy) {
            this.voice_energy = voice_energy;
        }

        public double getVoice_energy() {
            return voice_energy;
        }

    }
}

4.JsonUtil

package com.sunian.besjon;

import org.codehaus.jackson.map.ObjectMapper;

/**
 * @author sunian
 * @name speechRecognition
 * @class name:com.sunian.speechrecognition.util
 * @time 2018/11/7 10:15
 * @describe describe
 */
public class JsonUtil {

    public static String getJsonString(Object object) throws Exception {
        return JacksonMapper.getInstance().writeValueAsString(object);
    }

    public static Object toObject(String jsonString, Class cls) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(jsonString, cls);
    }

}

5.执行测试类

   String json = "{\n" +
        "\t\"results_recognition\": [\"沧州。\"],\n" +
        "\t\"origin_result\": {\n" +
        "\t\t\"corpus_no\": 6621008601394242097,\n" +
        "\t\t\"err_no\": 0,\n" +
        "\t\t\"result\": {\n" +
        "\t\t\t\"word\": [\"沧州。\"]\n" +
        "\t\t},\n" +
        "\t\t\"sn\": \"fbc63952-92f7-49cd-b044-5204afd7ba01_s-79\",\n" +
        "\t\t\"voice_energy\": 12175.6875\n" +
        "\t},\n" +
        "\t\"error\": 0,\n" +
        "\t\"best_result\": \"沧州。\",\n" +
        "\t\"result_type\": \"final_result\"\n" +
        "}";
    @Test
    public void getJsonRootBean() throws Exception {
        Object o = JsonUtil.toObject(json, JsonRootBean.class);
        System.out.println(o);
    }

6.将json转换成实体类

今天的文章json转换成实体类分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/30705.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注