1. 程式人生 > >json格式轉為 map 和 bean

json格式轉為 map 和 bean

gets style exceptio 2018年 ant import imp reserve new

JSONUtils 工具類

package com.xxx.qrcode;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

/**
 * 
 * @ClassName:JSONUtils
 * @Description:TODO(這裏用一句話描述這個類的作用    JSON工具類)
 * @author: ZHOUPAN
 * @date 2018年10月24日 上午11:58:29
 *
 * @Copyright: 2018 www.xxx.com Inc. All rights reserved.
 */
public class JSONUtils {
     private static ObjectMapper mapper;
    /**
     * 獲取ObjectMapper實例
     * @param createNew 方式:true,新實例;false,存在的mapper實例
     * @return
     */
    public static synchronized ObjectMapper getMapperInstance(boolean createNew) {
        if (createNew) {
            return new ObjectMapper();
        } else if (mapper == null) {
            mapper = new ObjectMapper();
        }
        return mapper;
    }

    /**
     * 將java對象轉換成json字符串
     * @param obj 準備轉換的對象
     * @return json字符串
     * @throws Exception
     */
    public static String beanToJson(Object obj) throws IOException {
        ObjectMapper objectMapper = getMapperInstance(false);
        String json =objectMapper.writeValueAsString(obj);
        return json;
    }

    /**
     * 將java對象轉換成json字符串
     * @param obj 準備轉換的對象
     * @param createNew ObjectMapper實例方式:true,新實例;false,存在的mapper實例
     * @return json字符串
     * @throws Exception
     */
    public static String beanToJson(Object obj,Boolean createNew) throws IOException {
        ObjectMapper objectMapper = getMapperInstance(createNew);
        String json =objectMapper.writeValueAsString(obj);
        return json;
    }
    
    /***
     * 
     * @Title: jsonToMap   
     * @Description: TODO(將JSON字符串轉為Map) 
     * @author: ZHOUPAN  
     * @param: @param jsonString
     *                                 JSON字符串
     * @param: @throws JSONException      
     *                                 異常處理
     * @return: Map
<String,Object> * 返回Map * @throws */ public static Map<String, Object> jsonToMap(String jsonString) throws JSONException { JSONObject jsonObject = JSONObject.fromObject(jsonString); Map<String, Object> result = new HashMap<
String, Object>(); Iterator<?> iterator = jsonObject.keys(); String key = null; String value = null; while (iterator.hasNext()) { key = (String) iterator.next(); value = jsonObject.getString(key); result.put(key, value); } return result; } /** * * @Title: jsonToBean * @Description: TODO(將JSON字符創轉為Bean) * @author: ZHOUPAN * @param: @param jsonString * @param: @param model * @param: @return * @param: @throws JSONException * @return: T * @throws */ @SuppressWarnings({ "static-access", "unchecked" }) public static <T> T jsonToBean(String jsonString, Class<T> model) throws JSONException { JSONObject jsonObject = JSONObject.fromObject(jsonString); Object object = jsonObject.toBean(jsonObject, model); return (T) object; } }

json格式轉為 map 和 bean