asp解析json序列(推荐)

[ 2009-11-16 09:03:36 | 作者: admin ]
字号: | |
p.s. 将步骤一中的代码先复制到页面中,然后进行步骤二中的调用,vbscript可以使用jscript定义的函数


步骤一、使用jscript做核心函数
<script language="JScript" runat="Server">
function toObject(json) {
         eval("var o=" + json);
         return o;
}
function toArray(json){
         var dic = Server.CreateObject("Scripting.Dictionary")
         eval("var a=" + json);
         for(var i=0;i<a.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>

步骤二、vbscript做表现脚本
<%
Dim json
json ="[{'uid':'1','username':'abc','email':'123@163.com'},{'uid':'2','username':'dbc','email':'456@163.com'}]"
Set json = toArray(json)
For i=0 To json.Count-1
     Response.Write json(i)("uid") & " <br/>"
     Response.Write json(i)("username") & " <br/>"
     Response.Write json(i)("email") & " <br/>"
Next
Set json = Nothing
%>

参考:
www.json.org
http://code.google.com/p/aspjson/
http://www.webdevbros.net/2007/04/26/generate-json-from-asp-datatypes/
[最后修改由 admin, 于 2016-09-30 16:54:33]
评论Feed 评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=1318

浏览模式: 显示全部 | 评论: 1 | 引用: 0 | 排序 | 浏览: 3720
引用 沉冰浮水*
[ 2011-04-11 08:16:44 ]
通常JSON数据不可能只含数组吧。。而要用第二个函数必须把数组部分截取出来。。

此日志不可发表评论。