jsonをエンティティクラスに変換

6011 ワード

今日同僚から質問がありました.彼女は文字列の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 = "{
" + "\t\"results_recognition\": [\" 。\"],
" + "\t\"origin_result\": {
" + "\t\t\"corpus_no\": 6621008601394242097,
" + "\t\t\"err_no\": 0,
" + "\t\t\"result\": {
" + "\t\t\t\"word\": [\" 。\"]
" + "\t\t},
" + "\t\t\"sn\": \"fbc63952-92f7-49cd-b044-5204afd7ba01_s-79\",
" + "\t\t\"voice_energy\": 12175.6875
" + "\t},
" + "\t\"error\": 0,
" + "\t\"best_result\": \" 。\",
" + "\t\"result_type\": \"final_result\"
" + "}"; @Test public void getJsonRootBean() throws Exception { Object o = JsonUtil.toObject(json, JsonRootBean.class); System.out.println(o); }

6.jsonをエンティティークラスに変換