zoukankan      html  css  js  c++  java
  • ASP 解析json

    第一个方法是使用 JScript :

    <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 />" Response.Write obj.c.length & "<br />" Response.Write obj.c.get(0) & "<br />" Set obj = Nothing %> <script language="JScript" runat="Server"> function ToObject(json) { var o; eval("o=" + json); return o; } </script> <% set obj1 = ToObject("{aaa:""aaaa"", bbb: ""bbbb""}") Response.Write obj1.aaa & "<br />" %>

      

    第二个方法是使用MS的脚本控件(也一样是使用了 JScript):

    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
    

      

  • 相关阅读:
    设计模式
    设计模式
    设计模式
    设计模式
    设计模式
    设计模式
    OpenGL学习 (一)
    C#
    Unity
    [开发ing] Unity项目
  • 原文地址:https://www.cnblogs.com/wgscd/p/10954260.html
Copyright © 2011-2022 走看看