前台如何传递json数据库,前台如何传递json数据库文件

发布时间:2023-12-08

前台如何传递json数据库,前台如何传递json数据库文件

更新:2022-11-19 09:19

本文目录一览:

  1. 后台json数据传到前台怎么传
  2. [前台能不能给后台传这样的json数据:一个json内有个值和一个json数组 请问前台给怎么传,后台该怎么接收](#前台能不能给后台传这样的json数据:一个json内有个值和一个json数组 请问前台给怎么传,后台该怎么接收)
  3. 如何将数据以json格式传给前端
  4. 如何在前台脚本通过json传递数据到后台

后台json数据传到前台怎么传

假设后台传递到前台数据的临时对象为json。

var json = "{……}";
json = eval('(' + json + ')');

json就对前台对象了,json.endPlncre可以拿出指定属性。

前台能不能给后台传这样的json数据:一个json内有个值和一个json数组 请问前台给怎么传,后台该怎么接收

你很懒。 第一步,封装json。

var jsonStr = [{'name':'jim' , 'age':20} , {'name':'king' , 'age':26},{'name':'jge' , 'age':30}]

第二步,使用AJAX。

jQuery.ajax({
    type: "post",
    url: url,
    dataType : 'json',
    data : {'mydata':jsonStr},
    success: function(data,textStatus){
        alert("操作成功");
    },
    error: function(xhr,status,errMsg){
        alert("操作失败!");
    }
});

第三步,后台方法接收。

String jsonStr = ServletActionContext.getRequest().getParameter("mydata");
JSONArray jsonArray = JSONArray.fromObject(jsonStr);
for(int i=0; i < jsonArray.length(); i++){
    JSONObject jsonJ = jsonArray.getJSONObject(i);
    jsonJ.getInt("name");
    jsonJ.getString("age");
}

.......ok不。

如何将数据以json格式传给前端

将数据以json格式传给前端:

function generateDtb() {
    //写入
    var txtName = document.getElementById("txtName").value;
    //创建数组
    var dtb = new Array();
    //通过循环把数据写入到数组并返回
    for (var i = 0; i < firstGroup.length; i++) {
        var row = new Object();
        row.Name = txtName;
        row.fullMoney = firstGroup[i].value;
        row.discount = secondGroup[i].value;
        dtb.push(row);
    }
    return dtb;
}

把数组转换成json串传入到后台:

$(function () {
    //点击botton1
    $("#lbtnOK").click(function () {
        var url = "DiscountManger.aspx?ajax=1";
        var dtb = generateDtb();
        // var strName = document.getElementById("txtName").value;
        if (dtb == null) {
        } else {
            //序列化对象
            var postdata = JSON.stringify(dtb);
            //异步请求
            $.post(url, { json: postdata }, function (json) {
                if (json) {
                    jBox.tip("添加成功!", "提示");
                    location.reload();
                } else {
                    jBox.tip("添加失败!", "提示");
                    location.reload();
                }
            }, "json")
        }
    });
});

在后台的操作: 首先判断是否需要传输数据

if (!IsPostBack) {
    //判断是否异步请求
    if (Request.QueryString["ajax"] == "1") {
        ProcessRequest();
    }
}

在这里进行对数据的处理:

/// <summary>
/// 处理异步请求
/// </summary>
private void ProcessRequest() {
    //存入要填写的策略
    ArrayList arrDiscount = new ArrayList();
    Response.ContentType = "text/html";
    string json = Request.Form["json"];
    //反序列化DataTable
    if (json == null) {
        return;
    } else {
        DataTable newdtb = Json2Dtb(json);
        for (int i = 0; i < newdtb.Rows.Count; i++) {
            Entity.StrategyDiscount enStrategyDiscount = new Entity.StrategyDiscount();
            //打折方案名
            enStrategyDiscount.name = newdtb.Rows[i]["Name"].ToString();
            //商店ID
            enStrategyDiscount.shopId = long.Parse(LoginInfo.ShopID);
            enStrategyDiscount.fullMoney = Convert.ToDecimal(newdtb.Rows[i]["fullMoney"].ToString());
            enStrategyDiscount.discount = Convert.ToDecimal(newdtb.Rows[i]["discount"].ToString());
            //写入数据到数组
            arrDiscount.Add(enStrategyDiscount);
        }
        //写入数据到数据库
        IStrategyBLL strategy = new StrategyBLL();
        if (strategy.AddStrategyDiscount(arrDiscount)) {
            Response.Write("true");
            Response.End();
        } else {
            Response.Write("false");
            Response.End();
        }
    }
}

这里,我们需要把json转换成datatable

/// <summary>
/// Json转DataTable
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
private DataTable Json2Dtb(string json) {
    JavaScriptSerializer jss = new JavaScriptSerializer();
    ArrayList dic = jss.Deserialize<ArrayList>(json);
    DataTable dtb = new DataTable();
    if (dic.Count > 0) {
        foreach (Dictionary<string, object> drow in dic) {
            if (dtb.Columns.Count == 0) {
                foreach (string key in drow.Keys) {
                    dtb.Columns.Add(key, drow[key].GetType());
                }
            }
            DataRow row = dtb.NewRow();
            foreach (string key in drow.Keys) {
                row[key] = drow[key];
            }
            dtb.Rows.Add(row);
        }
    }
    return dtb;
}

这样,就可以把数据无刷新的写入到数据库。 当然,如果我们有一个从数据库读取的datatable,如果通过json显示在前台呢。 首先,我们需要把datatable转换为json数据

/// <summary>
/// DataTable转Json
/// </summary>
/// <param name="dtb"></param>
/// <returns></returns>
private string Dtb2Json(DataTable dtb) {
    JavaScriptSerializer jss = new JavaScriptSerializer();
    ArrayList dic = new ArrayList();
    foreach (DataRow row in dtb.Rows) {
        Dictionary<string, object> drow = new Dictionary<string, object>();
        foreach (DataColumn col in dtb.Columns) {
            drow.Add(col.ColumnName, row[col.ColumnName]);
        }
        dic.Add(drow);
    }
    return jss.Serialize(dic);
}

然后写回到前台

/// <summary>
/// 处理异步请求
/// </summary>
private void ProcessRequest() {
    Response.ContentType = "text/html";
    string json = Request.Form["json"];
    //反序列化DataTable
    DataTable newdtb = Json2Dtb(json);
    //序列化DataTable为JSON
    string back = Dtb2Json(newdtb);
    Response.Write(back);
    Response.End();
}

在前台接受显示:

$(function() {
    //点击botton1
    $("#botton1").click(function() {
        createTable(json);
    });
});
//显示Json中的数据
function createTable(json) {
    var table = $("<table border='1'></table>");
    for (var i = 0; i < json.length; i++) {
        o1 = json[i];
        var row = $("<tr></tr>");
        for (key in o1) {
            var td = $("<td></td>");
            td.text(o1[key].toString());
            td.appendTo(row);
        }
        row.appendTo(table);
    }
    table.appendTo($("#back"));
}

如何在前台脚本通过json传递数据到后台

简单的说你的这个问题不能是“PHP读JS文件的数据”,而是“PHP怎么识别JS提交的数组类数据”。因为PHP一般没法直接识别JS的数组,所以你在定义你的这个JS数组的时候要使用 var jsonArray = {"1":"test", "1232":"test2"} 这种方式来定义一个JSON数组。之后你就可以自己写个 AJAX 方法请求你的那个处理的页面,并且把这个 JSON 数组传递过去。最后你在你的PHP脚本里面把这个数组用json_decode()方法将这个JSON数组转换成PHP的数组就行了。不过json_decode()只在PHP5.2.0之后的版本内才有集成。如果你的版本比较低就需要你自己去集成进去。