本文目錄一覽:
asp 怎麼接收post過來的json數據,解析出來後寫入數據庫?
script language=”jscript” runat=”server”
Array.prototype.get = function(x) { return this[x]; };
function parseJSON(strJSON) { return eval(“(” + strJSON + “)”); }
/script
%
Dim json, obj
json = “{a:””aaa””, b:{ name:””bb””, value:””text”” }, c:[“”item0″”, “”item1″”, “”item2″”]}”
Set obj = parseJSON(json)
Response.Write obj.a “br /” ‘直接獲取
Response.Write obj.b.name “br /” ‘獲取指定key
Response.Write obj.c.length “br /” ‘獲取條數
Response.Write obj.c.get(0) “br /” ‘C的第一條
Response.Write obj.c “br /” ‘獲取全部
Set obj = Nothing
%
然後就是對應的更新到數據庫就行了。
建議讓JSON,整條的保存在數據庫,取出來的時候這樣解釋就行了。。。
PHP接收json 並將接收數據插入數據庫的實現代碼
最近有一個需求,前端向後台提交json,後台解析並且將提交的值插入數據庫中,
難點
1、php解析json(這個不算難點了,網上實例一抓一大把)
2、解析json後,php怎樣拿到該拿的值
?php
require
(‘connect.php’);
/*
本例用到的數據:
post_array={“order_id”:”0022015112305010013″,”buyer_id”:”2″,”seller_id”:”1″,”all_price”:”100.00″,”json_list”:[{“product_id”:”3″,”product_number”:”3″},{“product_id”:”8″,”product_number”:”2″},{“product_id”:”10″,”product_number”:”4″}]}
*/
$post_array=$_POST[‘post_array’];
//–解析Json,獲取對應的變量值
$obj=json_decode($post_array,TRUE);
$order_id
=
$obj[‘order_id’];
$buyer_id
=
$obj[‘buyer_id’];
$seller_id
=
$obj[‘seller_id’];
$all_price
=
$obj[‘all_price’];
$i=0;//循環變量
//–得到Json_list數組長度
$num=count($obj[“json_list”]);
//–遍曆數組,將對應信息添加入數據庫
for
($i;$i$num;$i++)
{
$list_product_id[]=$obj[“json_list”][$i][“product_id”];
$list_product_number[]=$obj[“json_list”][$i][“product_number”];
$insert_order_product_sql=”INSERT
INTO
tbl_order_product
(order_id,product_id,product_number)
VALUES
(?,?,?)”;
$result
=
$sqlconn
–
prepare($insert_order_product_sql);
$result
–
bind_param(“sss”,
$order_id,$list_product_id[$i],$list_product_number[$i]);
$result-execute();
}
//–添加訂單信息
$insert_order_sql=”INSERT
INTO
tbl_order
(order_id,buyer_id,seller_id,all_price)
VALUES
(?,?,?,?)”;
$result=$sqlconn-prepare($insert_order_sql);
$result-bind_param(“ssss”,$order_id,$buyer_id,$seller_id,$all_price);
$result-execute();
$result
–
close();
$sqlconn
–
close();
?
投稿者信息
昵稱:
Hola
Email:
jamcistos@outlook.com
網頁製作時怎麼接收從數據庫送來的json
json數據一般是從web服務器送出的,其中的業務邏輯層可能需要從數據庫中調用相關數據,經過一系列的處理之後,在一次瀏覽器發出的http請求之後,返回給客戶端瀏覽器,你需要解析返回的json數據,然後展示出來,一般有ajax框架代勞了
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/236568.html