后台webservice返回JSON,前台js用ajax调用

4 篇文章 0 订阅

一、js将string转为json

http://blog.csdn.net/jjzjjz1/article/details/6334415

        function jsontest() {
            //var testJson = "{ name: '小强', age: 16 }";(支持)  
            //var testJson = "{ 'name': '小强', 'age': 16 }";(支持)  
            var testJson = '{ "name": "小强", "age": 16 }';
            alert(testJson);
            //testJson=eval(testJson);//错误的转换方式  
            testJson = eval("(" + testJson + ")");
            alert(testJson);
            alert(testJson.name);  
        }
二、后台webservice返回JSON,前台js用ajax调用
1、eg:

(1)webservice中的方法:

/// <summary>
        /// 根据地区获取客运驾驶舱汇总数据
        /// </summary>
        /// <param name="strAreaCode"></param>
        /// <returns></returns>
        [WebMethod]
        public static string GetPassengerCockpitByAreaCode(string strAreaCode, string strOrgId)
        {  
            StringBuilder sb = new StringBuilder();
            sb.Append("{");

            //1.客运车辆运力
            sb.Append("\"vehicle_num\": \"" + BaseBLLFactory.CreateService<JstmbsCacheService>().GetAreaPracPassengerSummaryByAreaCode(strAreaCode).Total + "\",");
            sb.Append("\"vehicle_seats\": \"2\","); 
            sb.Append("}");
             
            return sb.ToString();
        }

(2)js:

//根据AreaCode获取获取数据
function getPassCockpitData(strAreaCode_Cockpit,strOrgId_Cockpit) {
    //数据切换
    $.ajax({
        type: "POST",     //要用post方式     
        url: "../../Ajax/WebServiceDCHandler.aspx/GetPassengerCockpitByAreaCode",
        contentType: "application/json",
        data: '{"strAreaCode":"' + strAreaCode_Cockpit + '","strOrgId":"' + strOrgId_Cockpit + '"}',
        dataType: "json",
        success: function (data) {
            var cockpit_data = eval("(" + data.d + ")");
            alert(cockpit_data.vehicle_num);
            alert(cockpit_data.vehicle_seats);
        },
        error: function (err) {
            alert("获取【客运车辆数】数据出错!");
        }
    });
}

2、Ilist转化为json

(1)webservice中的方法:

        [WebMethod]
        public static string GetOwnerWithVehicleByOrgId(string strAreaCode, string strOrgId)
        {
            IList lstOwnerNumWithVehicleSum = BaseBLLFactory.CreateService<PassTransService>().GetOwnerWithVehicleByOrgId(strOrgId);

            StringBuilder sbJson = new StringBuilder();
            sbJson.Append("{\"OwnerWithVehicle\":[");

            for (int i = 0; i < lstOwnerNumWithVehicleSum.Count; i++)
            {
                Object[] lstObj = (Object[])lstOwnerNumWithVehicleSum[i];

                sbJson.Append("{");
                sbJson.Append("\"statisticsTarget\":\"" + lstObj[1].ToString() + "\",");
                sbJson.Append("\"ownerTotalNum\":\"" + lstObj[3].ToString() + "\",");
                sbJson.Append("\"E100\":\"" + lstObj[4].ToString() + "\",");
                sbJson.Append("\"E50\":\"" + lstObj[5].ToString() + "\",");
                sbJson.Append("\"E10\":\"" + lstObj[6].ToString() + "\",");
                sbJson.Append("\"E0\":\"" + lstObj[7].ToString() + "\""); 
                sbJson.Append("}");

                if (i < lstOwnerNumWithVehicleSum.Count - 1)
                {
                    sbJson.Append(",");
                }
            }
             
            sbJson.Append("]}");

            return sbJson.ToString();
        }

(2)js:

        function GetPassOwnerWithVehicleData() {
            var strAreaCode = getAreaCodeCookie();
            var strOrgId = getOrgIdCookie(); 

            $.ajax({
                type: "POST",     //要用post方式     
                url: "../../Ajax/WebServiceDCHandler.aspx/GetOwnerWithVehicleByOrgId",
                contentType: "application/json",
                data: '{"strAreaCode":"' + strAreaCode + '","strOrgId":"' + strOrgId + '"}',
                dataType: "json",
                success: function (data) { 
                    var allData = eval("(" + data.d + ")");
                    var series_data = [];
                    for (var i = 0; i < allData.OwnerWithVehicle.length; i++) {
                        var dataTemp = [];
                        dataTemp.push(parseFloat(allData.OwnerWithVehicle[i].E100));
                        dataTemp.push(parseFloat(allData.OwnerWithVehicle[i].E50));
                        dataTemp.push(parseFloat(allData.OwnerWithVehicle[i].E10));
                        dataTemp.push(parseFloat(allData.OwnerWithVehicle[i].E0));
                        series_data.push({ name: allData.OwnerWithVehicle[i].statisticsTarget, data: dataTemp });
                    }

                    var xAxis_data = ["100辆及以上企业", "50~99辆企业", "10~49辆企业", "10辆以下企业"];
                    var yAxis_title = "户";
                    column3DCharts("#container", xAxis_data, yAxis_title, series_data)
                },
                error: function (err) {
                    alert("获得客运业户车辆拥有量分析数据出错!");
                }
            });
        }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值