json字符串的传递

//定义对象
        var params = {}
        var result = [];
        $(".screenBox").each(function () {
            var id = $(this).attr('id');
            var obj = {};
            obj.ProjectCode = $("#ProjectCode_" + id + "").val();
            if ($("#UnitCode_" + id + "").val() == "") {
                obj.UnitCode = 0;
            } else {
                obj.UnitCode = $("#UnitCode_" + id + "").val();
            }
            obj.orientation = $("#houseOrientation_" + id + "").find("li.toward.active").val();
            obj.floor = $("#roomNo_" + id + "").val();
            //设置未查询到楼层,自行填写的层数
            obj.room = $("#roomNo").val();
            obj.reportRoom = $("#reportRoom_" + id + "").val();
            obj.totalFloor = $("#TotalFloor_" + id + "").val();
            obj.area = $("#coveredArea_" + id + "").val();
            obj.year = $("#completedYear_" + id + "").val();
            if ($("#UnitAddress_" + id + "").val() != "") {
                obj.address = $("#UnitAddress_" + id + "").val();
            } else {
                obj.address = $("#Address_" + id + "").val();
            }
            obj.reportServiceName = $("#areaName").val().split("-")[0];
            if ($("#buildingNo_" + id + "").val() != null) {
                obj.bulidingNo = $("#buildingNo_" + id + "").val().split("-")[0];
            }
            obj.serviceId = $('#serviceId').val();
            obj.userId = $('#userId').val();
            obj.datasourceName = $("#sourceName_" + id + "").val();
            obj.datasourceId = id;
            obj.sourceOriginal = $(this).parents().find("#sourceOriginal_" + id + "").val();
            obj.weightRatio = $(this).parents().find("#weightRatio_" + id + "").val();
            obj.rate = $("#rate").val();
            obj.houseType = $("#houseType").val();
            result.push(obj);
        })

        params.nyRealestateReportMult = JSON.stringify(result);
        $sj.tools.layerLoading();
        /*测试数据*/
        /*params.nyRealestateReportMult='[{"ProjectCode":"5a179538-646d-4cb5-857f-2a50ec51e53f","UnitCode":"2618c194-b64f-41f5-b368-f7f0fe025194","orientation":2,"floor":"2","reportRoom":"","totalFloor":"13","area":"100","year":"2011","address":"天山西路4358弄","reportServiceName":"爱博家园四期/爱博四村","bulidingNo":"10号","serviceId":"194","userId":"222","datasourceName":"上海城市评估","datasourceId":"1","sourceOriginal":"shanghai_city","weightRatio":"50","rate":"null"},{"ProjectCode":"27323","UnitCode":"650531","orientation":2,"floor":"2","reportRoom":"14370511","totalFloor":"13","area":"100","year":"2011","address":"","reportServiceName":"爱博家园四期/爱博四村","bulidingNo":"10号","serviceId":"194","userId":"222","datasourceName":"世联评估","datasourceId":"2","sourceOriginal":"shilian","weightRatio":"50","rate":"null"}]'*/
        /*[{"ProjectCode":"268084","UnitCode":"4000894","orientation":2,"reportRoom":"97051524","totalFloor":"23","area":"100","year":"2012","address":"","reportServiceName":"苏州湾壹号","bulidingNo":"4幢","serviceId":"197","userId":"222","datasourceName":"世联评估","datasourceId":"4","sourceOriginal":"shilian","weightRatio":"100","rate":"1.1"}]*/
        $.post(path + "queryCity/generateQueryReport", params, function (data) {
            $sj.tools.hideLoading();
            if (data.success == true) {
                $sj.tools.layerTip(data.msg || "提交成功", {}, function () {
                    location.href = path + "queryCity/queryReport/" + data.data;
                });
            } else {
                $sj.tools.layerTip(data.msg || "提交失败", {type: 2});
            }

        }, "json").error(function () {
            $sj.tools.layerTip("操作失败", {type: 2});
        });

    }

controller接受

@RequestMapping(value = "/generateQueryReport", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult generateQueryReport(String nyRealestateReportMult) {

        try {
          
            List<NyRealestateReportMult> List = GsonUtil.getInstance().g().fromJson(nyRealestateReportMult,
                    new TypeToken<List<NyRealestateReportMult>>() {
                    }.getType());
            /*List<NyRealestateReportMult> List = JsonUtil.JsonToList(nyRealestateReportMult, new TypeReference<List<NyRealestateReportMult>>() {});*/
 } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail("查询失败");
        }
    }
GsonUtil
package com.nuoya.zzx.extend.pojo;


import com.google.gson.Gson;

/**
 * Created by ty
 * on 2017/7/8.
 */
public class GsonUtil {
    private static GsonUtil instance;
    private Gson gson;

    private GsonUtil() {
        gson = new Gson();
    }

    public static GsonUtil getInstance() {
        if (instance == null) {
            synchronized (GsonUtil.class) {
                if (instance == null) {
                    instance = new GsonUtil();
                }
            }
        }
        return instance;
    }

    public static GsonUtil $() {
        return getInstance();
    }

    public Gson g(){
        return gson;
    }
    public static Gson gson(){
        return $().g();
    }
}

JsonUtil

package com.nuoya.zzx.util;

import java.io.IOException;
import java.util.List;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * Created by mingliang on 2017/9/16.
 */
public class JsonUtil {
	
    /**
     * 获取泛型的Collection Type
     *
     * @param <T>
     * @param collectionClass 泛型的Collection
     * @param elementClasses  元素类
     * @return JavaType Java类型
     * @throws IOException
     * @throws JsonMappingException
     * @throws JsonParseException
     * @since 1.0
     */
    public static <T> List<T> JsonToList(String json, TypeReference<List<T>> type)
            throws JsonParseException, JsonMappingException, IOException {
        ObjectMapper mapper = new ObjectMapper();
        List<T> list = mapper.readValue(json, type);
        return list;
    }
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public static Object JsonToBean(String json, Class clazz)
    		throws Exception {
    	 GsonBuilder gsonBuilder = new GsonBuilder();  
         gsonBuilder.excludeFieldsWithoutExposeAnnotation();// 不转换没有 @Expose 注解的字段  
         Gson gson1 = gsonBuilder.create();  
    	 return gson1.fromJson(json, clazz);
    }
}

json字符串的另外一种方法

getItemList: function (filterFun) {   
            var result = [];
            if (filterFun != null && typeof(filterFun) == 'function') {
                for (var i = 0; i < result.length; i++) {
                    result[i] = filterFun(i, result[i]);
                }
            } else if (filterFun != null && typeof(filterFun) == 'string') {
                for (var i = 0; i < result.length; i++) {
                    var cache = {};
                    cache[filterFun] = result[i][filterFun];
                    result[i] = cache;
                }
            }
            return result;
        }
var borrowIds = JSON.stringify(borrowListTemple.getItemList("id"));

获取方法

if(state) {
            if(DataUtil.isNotEmpty(jsonBorrowerIds)) {
                try {
                    JSONArray jsonArray = JSONArray.parseArray(jsonBorrowerIds);
                    List<String> list = new ArrayList<>();
                    for(int i = 0; i < jsonArray.size(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        list.add(jsonObject.getString("id"));
                    }
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("list", list);
                    map.put("id", assetPool.getId());
                    map.put("createUid", createUid);
                    boolean insert = assetPoolExMapper.insertAssetPoolBorrowRelate(map) > 0;
                    if(insert) {
                        this.updateAssetPoolBorrowerMoney(createUid, assetPool.getId());
                    }
                } catch (Exception e) {
                }
            }
        }
json格式:
1. {“a”:"A","b":"B","c":"C"}键值对,类似于java中的map
2. json数组 [{“a”:"A","b":"B","c":"C"},{“a”:"A","b":"B","c":"C"},{“a”:"A","b":"B","c":"C"}]只要满足以上格式,可以自行组合,因此表示的数据就更复杂了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值