蓝羊羊的gravatar头像
蓝羊羊 2016-10-27 11:31:20

java如何解析json数据并且保存到数据库?

data字段格式
{
"phone": ["xxxxxxxx","xxxxxx","+xxxxxx"],
"qq": ["xxxxx", "xxxxx", "xxxx"],
"id_card": "xxxxxx",
"address": "xxxxxx"
}

求这样的json格式解析添加数据库方法,谢谢

所有回答列表(5)
程序猿全敏的gravatar头像
程序猿全敏  LV29 2016年10月27日

你可以看看我的分享啊!有专门的json转对象的工具类,当页面传数据到action,调用json转换工具类转换成Object对象,然后通过dao层save方法保存到数据库。给你一个

001package com.shscn;
002 
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.HashMap;
006import java.util.Iterator;
007import java.util.List;
008import java.util.Map;
009 
010import org.json.JSONArray;
011import org.json.JSONException;
012import org.json.JSONObject;
013 
014 
015/**
016 * JSON字符串生成类
017 * @author qm
018 */
019public class JsonUtil {
020    /** 实体类转换为JSON */
021    public static String toJson(Object obj){
022        if(obj == null) {
023            return "{}";
024        }
025        JSONObject jo = new JSONObject(obj);
026        return jo.toString();
027    }
028     
029    /** list转换为JSON */
030    public static String toJson(Collection<?> coll) {
031        if(coll == null) {
032            return "[]";
033        }
034        JSONArray ja = new JSONArray(coll);
035        return ja.toString();
036    }
037     
038    /** map转换为JSON */
039    public static <K, V> String toJson(Map<K, V> map) {
040        if(map == null) {
041            return "{}";
042        }
043        List<Map<K, V>> list = new ArrayList<Map<K, V>>();
044        list.add(map);
045        String str = JsonUtil.toJson(list);
046        return str.substring(1, str.length()-1);
047    }
048     
049    /**
050     * JSON转换为map
051     * @param json
052     * @return
053     */
054    @SuppressWarnings("unchecked")
055    public static <K, V> Map<K, V> toMap(String json) {
056        return new JSONObject(json).getMap();
057    }
058     
059     /**
060     * 将json字符串转换为List集合
061     * @param jsonArrStr
062     * @return
063     */
064    @SuppressWarnings("unchecked")
065    public static List<Map<String, Object>> jsonObjList(String jsonArrStr) {
066        List<Map<String, Object>> jsonList = new ArrayList<Map<String, Object>>();
067        JSONArray jsonArr = null;
068        try {
069            jsonArr = new JSONArray(jsonArrStr);
070            jsonList = (List<Map<String, Object>>)JsonUtil.jsonToList(jsonArr);
071        } catch (JSONException e) {
072            System.out.println("Json字符串转换异常!");
073            e.printStackTrace();
074        }
075        return jsonList;
076    }
077     
078    /**
079     * 将传递近来的json数组转换为List集合
080     * @param jsonArr
081     * @return
082     * @throws JSONException
083     */
084    private static List<?> jsonToList(JSONArray jsonArr)
085            throws JSONException {
086        List<Object> jsonToMapList = new ArrayList<Object>();
087        for (int i = 0; i < jsonArr.length(); i++) {
088            Object object = jsonArr.get(i);
089            if (object instanceof JSONArray) {
090                jsonToMapList.add(JsonUtil.jsonToList((JSONArray) object));
091            } else if (object instanceof JSONObject) {
092                jsonToMapList.add(JsonUtil.jsonToMap((JSONObject) object));
093            } else {
094                jsonToMapList.add(object);
095            }
096        }
097        return jsonToMapList;
098    }
099     
100    /**
101     * 将传递近来的json对象转换为Map集合
102     * @param jsonObj
103     * @return
104     * @throws JSONException
105     */
106    @SuppressWarnings("unchecked")
107    private static Map<String, Object> jsonToMap(JSONObject jsonObj)
108            throws JSONException {
109        Map<String, Object> jsonMap = new HashMap<String, Object>();
110        Iterator<String> jsonKeys = jsonObj.keys();
111        while (jsonKeys.hasNext()) {
112            String jsonKey = jsonKeys.next();
113            Object jsonValObj = jsonObj.get(jsonKey);
114            if (jsonValObj instanceof JSONArray) {
115                jsonMap.put(jsonKey, JsonUtil.jsonToList((JSONArray) jsonValObj));
116            } else if (jsonValObj instanceof JSONObject) {
117                jsonMap.put(jsonKey, JsonUtil.jsonToMap((JSONObject) jsonValObj));
118            } else {
119                jsonMap.put(jsonKey, jsonValObj);
120            }
121        }
122        return jsonMap;
123    }
124     
125}
评论(0) 最佳答案
dahjkhk的gravatar头像
dahjkhk  LV2 2016年10月27日

http://blog.csdn.net/my_lord_/article/details/50612863

咖啡没有猫的gravatar头像
咖啡没有猫  LV2 2016年10月27日

json 转对象

zdd123456的gravatar头像
zdd123456  LV9 2016年10月28日

JSONOBJECT

zuiyang的gravatar头像
zuiyang  LV2 2016年11月3日
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${fasterxml.version}</version>
</dependency>
ds 
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友