解析java接收json数据时有空数据怎么在catch快中给赋值

&&完java如何解析json数据并且保存到数据库?data字段格式{&phone&: [&xxxxxxxx&,&xxxxxx&,&+xxxxxx&],&qq&: [&xxxxx&, &xxxxx&, &xxxx&],&id_card&: &xxxxxx&,&address&: &xxxxxx&}求这样的json格式解析添加数据库方法,谢谢5个牛币所有回答列表(5)&LV3你可以看看我的分享啊!有专门的json转对象的工具类,当页面传数据到action,调用json转换工具类转换成Object对象,然后通过dao层save方法保存到数据库。给你一个
package com.
import java.util.ArrayL
import java.util.C
import java.util.HashM
import java.util.I
import java.util.L
import java.util.M
import org.json.JSONA
import org.json.JSONE
import org.json.JSONO
* JSON字符串生成类
* @author qm
public class JsonUtil {
/** 实体类转换为JSON */
public static String toJson(Object obj){
if(obj == null) {
return &{}&;
JSONObject jo = new JSONObject(obj);
return jo.toString();
/** list转换为JSON */
public static String toJson(Collection&?& coll) {
if(coll == null) {
return &[]&;
JSONArray ja = new JSONArray(coll);
return ja.toString();
/** map转换为JSON */
public static &K, V& String toJson(Map&K, V& map) {
if(map == null) {
return &{}&;
List&Map&K, V&& list = new ArrayList&Map&K, V&&();
list.add(map);
String str = JsonUtil.toJson(list);
return str.substring(1, str.length()-1);
* JSON转换为map
* @param json
@SuppressWarnings(&unchecked&)
public static &K, V& Map&K, V& toMap(String json) {
return new JSONObject(json).getMap();
* 将json字符串转换为List集合
* @param jsonArrStr
@SuppressWarnings(&unchecked&)
public static List&Map&String, Object&& jsonObjList(String jsonArrStr) {
List&Map&String, Object&& jsonList = new ArrayList&Map&String, Object&&();
JSONArray jsonArr =
jsonArr = new JSONArray(jsonArrStr);
jsonList = (List&Map&String, Object&&)JsonUtil.jsonToList(jsonArr);
} catch (JSONException e) {
System.out.println(&Json字符串转换异常!&);
e.printStackTrace();
return jsonL
* 将传递近来的json数组转换为List集合
* @param jsonArr
* @throws JSONException
private static List&?& jsonToList(JSONArray jsonArr)
throws JSONException {
List&Object& jsonToMapList = new ArrayList&Object&();
for (int i = 0; i & jsonArr.length(); i++) {
Object object = jsonArr.get(i);
if (object instanceof JSONArray) {
jsonToMapList.add(JsonUtil.jsonToList((JSONArray) object));
} else if (object instanceof JSONObject) {
jsonToMapList.add(JsonUtil.jsonToMap((JSONObject) object));
jsonToMapList.add(object);
return jsonToMapL
* 将传递近来的json对象转换为Map集合
* @param jsonObj
* @throws JSONException
@SuppressWarnings(&unchecked&)
private static Map&String, Object& jsonToMap(JSONObject jsonObj)
throws JSONException {
Map&String, Object& jsonMap = new HashMap&String, Object&();
Iterator&String& jsonKeys = jsonObj.keys();
while (jsonKeys.hasNext()) {
String jsonKey = jsonKeys.next();
Object jsonValObj = jsonObj.get(jsonKey);
if (jsonValObj instanceof JSONArray) {
jsonMap.put(jsonKey, JsonUtil.jsonToList((JSONArray) jsonValObj));
} else if (jsonValObj instanceof JSONObject) {
jsonMap.put(jsonKey, JsonUtil.jsonToMap((JSONObject) jsonValObj));
jsonMap.put(jsonKey, jsonValObj);
return jsonM
最佳答案http://blog.csdn.net/my_lord_/article/details/json 转对象JSONOBJECT
&dependency&
&groupId&com.fasterxml.jackson.core&/groupId&
&artifactId&jackson-databind&/artifactId&
&version&${fasterxml.version}&/version&
&/dependency&
ds 等等等等等等等等等等等等最热搜索问答话题编程语言基础Web开发数据库开发客户端开发脚本工具游戏开发服务器软硬件开源组件类库相关问答等等等等完等等等等等等等最近浏览暂无贡献等级暂无贡献等级暂无贡献等级暂无贡献等级暂无贡献等级暂无贡献等级暂无贡献等级暂无贡献等级暂无贡献等级扫描二维码关注最代码为好友"/>扫描二维码关注最代码为好友网络数据解析JSON_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
网络数据解析JSON
上传于|0|0|文档简介
&&解析网络URL的数据信息,获取网络JSON数据,非JSON数据的内容,本地json数据
你可能喜欢

我要回帖

更多关于 java接收json数据 的文章

 

随机推荐