本文目錄一覽:
ASP讀取JSON數組的問題。求解啊!!!!
script language=”JScript” runat=”Server”
function ToObject(json) {
var o;
eval(“o=” + json);
return o;
}
function toArray(s){
var dic = Server.CreateObject(“Scripting.Dictionary”)
eval(“var a=” + json);
for(var i=0;ia.length;i++){
var obj = Server.CreateObject(“Scripting.Dictionary”)
for(x in a[i]) obj.Add(x,a[i][x])
dic.Add(i, obj);
}
return dic
}
/script
%
json = “[{“”date””:””周四 08月07日 (實時:2)””,””weather””:””晴””,””wind””:””微風””,””temperature””:””21″”},{“”date””:””周五””,””weather””:””多雲””,””wind””:””微風””,””temperature””:””31 ~ 22″”},{“”date””:””周六””,””weather””:””多雲轉陰””,””wind””:””微風””,””temperature””:””30 ~ 22″”},{“”date””:””周日””,””weather””:””陰轉晴””,””wind””:””微風””,””temperature””:””31 ~ 22″”}]”
Set ob = toArray(json)
For i=0 To ob.Count-1
Response.Write ob(i)(“date”) ” br/”
next
Set ob = Nothing
%
注意JSON字元串前後的 [ ]
asp 如何請求 json
傳統的ASP與ASP之間post提交json可以用:
json=cstr(request.form)
來獲取得到的json代碼
2
實際上,如果是java或php提交過來的話,用request.form可能得到的就是空值,最穩妥的辦法是根據二進位流得到數據,具體操作如下:
3
2個頁面,第一個頁面假設為:funtion.asp
代碼如下:
%
function bytes2bstr(vin)
dim bytesstream,stringreturn
set bytesstream = server.CreateObject(“adodb.stream”)
bytesstream.type = 2
bytesstream.open
bytesstream.writeText vin
bytesstream.position = 0
bytesstream.charset = “utf-8″‘或者gb2312
bytesstream.position = 2
stringreturn = bytesstream.readtext
bytesstream.close
set bytesstream = nothing
bytes2bstr = stringreturn
end function
%
4
第二個頁面,假設為demo.asp,代碼如下:
!–#include file=”funtion.asp”–
%
getpostjson=Request.TotalBytes ‘得到位元組數
if getpostjson=0 then
response.Write(“json null”)
response.End()
end if
readjson=Request.BinaryRead(getpostjson) ‘二進位方式來讀取客戶端使用POST傳送方法所傳遞的數據
json = bytes2bstr(readjson) ‘二進位轉化
response.write(json)
%
5
字元串解析:
Set jsonobj=getJSONObject(json)
json數據請問怎麼遍歷
如果是js中遍歷使用
var anObject = {one:1,two:2,three:3};//對json數組each
$.each(anObject,function(name,value) {
});
如果是Java代碼直接用for循環就行了,說白了json也是數組的一種,json對象和json數組都可以
//遍歷json數組
String json1 = “{data:[{name:’Wallace’},{name:’Grommit’}]}”;
jsonObjSplit = new JSONObject(json1);
JSONArray ja = jsonObjSplit.getJSONArray(“data”);
for (int i = 0; i ja.length(); i++) {JSONObject jo = (JSONObject) ja.get(i);System.out.println(jo.get(“name”));}
//JSONObject遍歷json對象
String json2 = “{name:’Wallace’,age:15}”;
jsonObj = new JSONObject(json2);
for (Iterator iter = jsonObj.keys(); iter.hasNext();) {String key = (String)iter.next();System.out.println(jsonObj .getString(Key));}
ASP 讀取json後如何 寫入資料庫
ASP 獲取JSON 數據:script language=”JScript” runat=”Server”
function toObject(json) {
eval(“var o=” + json);
return o;
}
/script
%
Dim json
json =”{“”px_name””:””第二屆””,””px_ksjs””:””2014-03-11″”,””px_kcfl””:””培訓課程””}”
Set json = toObject(json)
Response.Write json.px_name ” br/”
Response.Write json.px_ksjs ” br/”
Response.Write json.px_kcfl ” br/”
Set json = Nothing
%
2.寫入資料庫
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|db1.mdb”;
OleDbCommand comm = new OleDbCommand();
conn.Open();
comm.Connection = conn;
comm.CommandText = “Insert Into [Time] ([begin],[over],[name]) Values (@begin,@over,@name)”;
comm.Parameters.AddWithValue(“@begin”, a);
comm.Parameters.AddWithValue(“@over”, b);
comm.Parameters.AddWithValue(“@name”, c);
comm.ExecuteNonQuery();
conn.Close();
注意:
1、以上代碼是以access數據為例。
2、如果表裡面有其他不能為空的欄位存在,需要給他們提供值,自動增加的欄位除外。
3、db1.mdb文件的只讀屬性去掉,在文件的屬性-安全性裡面,添加 everyone帳號和NETWORK Service 帳號可修改許可權。
問高手:asp如何處理json字元串
asp的默認腳本語言是VBScript,但實際上它也是支持JScript(這是微軟搞的伺服器版的JavaScript,語法基本上與JavaScript一樣)的,如果你用JScript作為asp的腳本語言,那麼處理json字串就太簡單了,因為它是原生支持json對象的,比如:
%@LANGUAGE=”JSCRIPT” CODEPAGE=”65001″%
%
var json=eval(“(“+Request(“data”)+”)”);
Response.write(“p”+json.a+”/p”);
Response.write(“p”+json.b[1]+”/p”);
%
測試方法:在網址後面添加參數 ?data={a:1,b:[2,3,4,5]}
如果堅持使用VBScript,那麼它是不支持json的,必須通過VBScript+JScript混合編程來實現:
script language=”jscript” runat=”server”
Array.prototype.get=function(x){
return this[x];
};
function parseJSON(strJSON){
return eval(“(“+strJSON+”)”);
}
/script
%
set json=parseJSON(request(“data”))
response.write “p” json.a “/p”
response.write “p” json.b.get(1) “/p”
%
請注意,這種情況下對json中的數組的處理方法跟通常是有所不同的。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/239681.html