asp如何解析json字符串并转化为asp对象

本文介绍如何使用asp解析json字符串,大家都知道asp对json的处理没有php那么简单,也没有键值对数组,这可能也是它最终被php取代的直接原因。

下面代码经本人测试可用,

代码如下:

Dim scriptCtrl
Function parseJSON(str)
    If Not IsObject(scriptCtrl) Then
        Set scriptCtrl = Server.CreateObject(MSScriptControl.ScriptControl)
        scriptCtrl.Language = JScript
        scriptCtrl.AddCode Array.prototype.get = function(x) { return this[x]; }; var result = null;
    End If
    scriptCtrl.ExecuteStatement result =  & str & ;
    Set parseJSON = scriptCtrl.CodeObject.result
End Function
Dim json
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 />
Response.Write obj.c.length & <br />
Response.Write obj.c.get(0) & <br />
Set obj = Nothing
Set scriptCtrl = Nothing

本文由编程之家提供,

文章地址:http://www.jb51.cc/csharp-article-377133.html

学编程就来编程之家 www.jb51.cc

相关文章

项目中经常遇到CSV文件的读写需求,其中的难点主要是CSV文件...
简介 本文的初衷是希望帮助那些有其它平台视觉算法开发经验的...
这篇文章主要简单记录一下C#项目的dll文件管理方法,以便后期...
在C#中的使用JSON序列化及反序列化时,推荐使用Json.NET——...
事件总线是对发布-订阅模式的一种实现,是一种集中式事件处理...
通用翻译API的HTTPS 地址为https://fanyi-api.baidu.com/api...