json转换成实体类

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

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\": ,\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转换成实体类

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/200344.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月20日 上午11:01
下一篇 2026年3月20日 上午11:01


相关推荐

  • springmvc实现拦截器两种方式_追逐拦截他人的定义

    springmvc实现拦截器两种方式_追逐拦截他人的定义SpringMVC拦截器1、什么是拦截器  在系统中,经常需要在处理用户请求之前和之后执行一些行为,例如检测用户的权限,或者将请求的信息记录到日志中,即平时所说的“权限检测”及“日志记录”。当然不仅仅这些,所以需要一种机制,拦截用户的请求,在请求的前后添加处理逻辑。  SpringMVC的拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理。将拦截器按一定的顺序联结成一条链,这条链称为拦截器链(InterceptorChain)。在访问被拦截的方法或字段时

    2025年8月11日
    2
  • 国产AI PC正式落地 科大讯飞发布星火AIPC

    国产AI PC正式落地 科大讯飞发布星火AIPC

    2026年3月14日
    1
  • LaTeX详细教程+技巧总结[通俗易懂]

    LaTeX详细教程+技巧总结[通俗易懂]LaTeX技巧总结,包含LaTeX工具,LaTeX模板,LaTeX技巧,LaTeX基础,文章架构,字体、大小和颜色,链接,列表,图片,表格,数学公式,算法(伪代码),代码块,论文写作,等等。

    2022年5月16日
    61
  • Cubieboard 架设Git服务器

    Cubieboard 架设Git服务器如果你现在用的是Cubieboard或者树莓派卡片式电脑,可以查看本文之前,学习前面的四个教程,它可能会对你非常有帮助。如果你是普通的Linux用户或者LinuxVPS、Linux独立服务器等,可以直接跳过查看本文。教程一Cubieboard安装Linux系统教程二CubieboardLinux服务器配置教程三CubieboardLinux服务器安装L…

    2022年7月22日
    9
  • RRC概述

    RRC概述转自 http www mscbsc com cidian baike66pRRC RadioResourc 无线资源控制协议 nbsp 概述其中 第一层是物理层 PhysicalLaye 第二层是媒介访问控制层 MediumAccess RRC 是第三层 RRC 对无线资源进行分配并发送相关信令 UE 和 UTRAN

    2026年3月17日
    3
  • js正则使用变量_js正则表达式语法大全

    js正则使用变量_js正则表达式语法大全正常有变量evalnewRegExp

    2025年12月4日
    4

发表回复

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

关注全栈程序员社区公众号