zoukankan      html  css  js  c++  java
  • 使用jackson解析JSON数据

    本文介绍使用jackson来对json数据进行解析操作

    首先,需要去官网下载jackson,本文使用的是(jackson-all-1.9.11.jar)

    主要通过ObjectMapper对json进行处理

     1 package com.whroid.java.json;
     2 
     3 import java.io.IOException;
     4 
     5 
     6 import org.codehaus.jackson.JsonFactory;
     7 import org.codehaus.jackson.JsonGenerationException;
     8 import org.codehaus.jackson.JsonParseException;
     9 import org.codehaus.jackson.map.JsonMappingException;
    10 import org.codehaus.jackson.map.ObjectMapper;
    11 import org.codehaus.jackson.map.SerializationConfig;
    12 
    13 
    14 public class JacksonUtil {
    15     
    16      ObjectMapper mObjectMapper;
    17     JsonFactory mJsonFactory;
    18     static JacksonUtil mJacksonUtil;
    19     public static JacksonUtil getInstance()
    20     {
    21         if(mJacksonUtil == null)
    22         {
    23             mJacksonUtil = new JacksonUtil();
    24         }
    25         return mJacksonUtil;
    26     }
    27     private JacksonUtil()
    28     {
    29         mObjectMapper = new ObjectMapper();
    30         mObjectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);//(禁止SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS)
    31         mJsonFactory = mObjectMapper.getJsonFactory();
    32     }
    33     
    34 
    35     /**
    36      * 将json字符串数据,转换成cls类实例。需要保证cls类中的属性和json中相对应,并且cls中必须有相应的getter()/setter()方法
    37      * @param json
    38      * @param cls
    39      * @return
    40      * @throws JsonParseException
    41      * @throws IOException
    42      */
    43     public   <T extends Object>  T  jsonToObject(String json,Class<T> cls) throws JsonParseException, IOException
    44     {
    45         return mObjectMapper.readValue(json, cls);
    46     }
    47     /**
    48      * 将实例对象,转化成json字符串。需要保证cls类中的属性和json中相对应,并且cls中必须有相应的getter()/setter()方法
    49      * @param obj
    50      * @return
    51      * @throws JsonGenerationException
    52      * @throws JsonMappingException
    53      * @throws IOException
    54      */
    55     public  String  objectToJson(Object obj) throws JsonGenerationException, JsonMappingException, IOException
    56     {
    57         return mObjectMapper.writeValueAsString(obj);
    58     }
    59 }

    与json转化的对象类JSONBean,需要添加getter()/setter()方法

     1 package com.whroid.java.json;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 
     7 public class JSONBean {
     8 
     9     private String name  = "name";
    10     private int id = 1;
    11     private List<String> list = new ArrayList<String>();
    12     private boolean b;
    13     SubBean subBean = new SubBean();
    14     public String getName() {
    15         return name;
    16     }
    17     public void setName(String name) {
    18         this.name = name;
    19     }
    20     
    21     public int getId() {
    22         return id;
    23     }
    24     public void setId(int id) {
    25         this.id = id;
    26     }
    27     
    28     public List<String> getList() {
    29         return list;
    30     }
    31     public void setList(List<String> list) {
    32         this.list = list;
    33     }
    34     public boolean isB() {
    35         return b;
    36     }
    37     public void setB(boolean b) {
    38         this.b = b;
    39     }
    40     public SubBean getSubBean() {
    41         return subBean;
    42     }
    43     public void setSubBean(SubBean subBean) {
    44         this.subBean = subBean;
    45     }
    46     @Override
    47     public String toString() {
    48         return "JSONBean [name=" + name + ", id=" + id + ", list=" + list
    49                 + ", b=" + b + ", subBean=" + subBean + "]";
    50     }
    51     
    52     
    53 }
    54 class SubBean{
    55     String subname = "subname";
    56 
    57     public String getSubname() {
    58         return subname;
    59     }
    60 
    61     public void setSubname(String subname) {
    62         this.subname = subname;
    63     }
    64 
    65     @Override
    66     public String toString() {
    67         return "SubBean [subname=" + subname + "]";
    68     }
    69     
    70 }

    调用

     1 public static void main(String[] args) {
     2         
     3         JSONBean bean = new JSONBean();
     4         String jackson = null;
     5         try {
     6             jackson = JacksonUtil.getInstance().objectToJson(bean);
     7             System.out.println("jackson:"+jackson);
     8             
     9             bean = JacksonUtil.getInstance().jsonToObject(jackson, JSONBean.class);
    10             System.out.println("bean:"+bean);
    11         } catch (Exception e) {
    12             e.printStackTrace();
    13         }
    14         
    15     }

    返回:

    jackson:{"name":"name","id":1,"list":[],"b":false,"subBean":{"subname":"subname"}}

    //使用上面那段json解析后的对象
    bean:JSONBean [name=name, id=1, list=[], b=false, subBean=SubBean [subname=subname]]

  • 相关阅读:
    生产宕机dunp配置
    虚拟机下载地址
    处理soapUI特殊返回报文 【原】
    SpringMVC 手动控制事务提交 【转】
    码云URL
    Java IO流操作汇总: inputStream 和 outputStream【转】
    springMVC下载中文文件名乱码【转】
    js
    js
    js
  • 原文地址:https://www.cnblogs.com/whroid/p/3576904.html
Copyright © 2011-2022 走看看