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


			org.codehaus.jackson
			jackson-mapper-lgpl
			1.9.13
			test

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 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 results_recognition) {
        this.results_recognition = results_recognition;
    }

    public List 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 word;

        public Result() {
        }

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

        public List 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转换成实体类

你可能感兴趣的:(java,javaEE,idea,springmvc)