2

次のように、ASP Classic で JSON の結果を取得しています。

<script language="JScript" runat="server" src="json2.js"></script>
<%
Response.ContentType = "text/html"

Dim HttpReq
Dim Diamond
Set HttpReq = Server.CreateObject("Msxml2.ServerXMLHTTP.6.0")
HttpReq.open "GET", "http://url.com?id=" & Request.QueryString("id"), False
HttpReq.send

res = HttpReq.responseText
Set res = JSON.parse(res)
%>

できます。

JSON の結果が次のようになるとします。

res = {
    gallery: { 
        image1: { img: 'source url', thumb: 'source url' },
        image2: { img: 'source url', thumb: 'source url' },
        image3: { img: 'source url', thumb: 'source url' }
    },
    another_param1: 'foo',
    another param2: 'bar',
    ...
}

次に、JScript ではなく VBScript でギャラリー オブジェクトを反復処理したいと考えています。

どうやってやるの?

事前にどうもありがとうございました。

4

3 に答える 3

2

VBScriptでオブジェクトのプロパティを列挙する必要がある場合は、オブジェクトをディクショナリに変換する必要があります。これは、JScriptオブジェクトを受け取って返す単純な関数ですScripting.Dictionary

<script runat="server" language="javascript">

    function JScriptObjectToDictionary(o)
    {
        var dict = new ActiveXObject("Scripting.Dictionary")

        for (var prop in o)
        {
            if (o.hasOwnProperty(prop))
            {
                dict.add(prop, o[prop]);
            }
        }

        return dict;
    }
</script>

この関数がページにあると、galleryプロパティを辞書に変換できるようになります。

 res.gallery = JScriptObjectToDictionary(res.gallery)

次に、次のように繰り返すことができます。

 For Each key in res.gallery
     Response.Write key & ": " & res.gallery(key).img & "<br />"
 Next

image1、image2のようなシリーズとして名前が付けられたプロパティは、不適切な選択であることを指摘する価値があると言っています。JSONでギャラリーをオブジェクトではなく単純な配列として定義した方がよいでしょう。JSONデザインに影響を与える立場にある場合は、JSONデザインを変更する必要があります。

于 2012-05-28T12:11:24.750 に答える
1

AXE(ASP Xtreme Evolution) JSON パーサーがそのトリックを実行します。グーグルで検索してください。ファイルに AX を含めたら、

set Info = JSON.parse(RES)
Response.write(Info.gallery & vbNewline)
Response.write(Info.gallery.image & vbNewline)

編集 - -

ギャラリーのループを作成する必要がある場合。

dim key : Info.keys() の各キーに対して Response.write( key & vbNewline ) next

于 2012-05-28T10:57:50.763 に答える
0

JSON が単純で、常に同じ構造で自分で解析できる場合、外部ライブラリは必要なく、高速かつ単純です。

res = "{"_
    &"gallery: { "_
    &"    image1: { img: 'source url1', thumb: 'source url1' },"_
    &"    image2: { img: 'source url2', thumb: 'source url2' },"_
    &"    image3: { img: 'source url3', thumb: 'source url3' }"_
    &"},"_
    &"another_param1: 'foo',"_
    &"another param2: 'bar'"_
    &"}"

'convert the JSON string to an array
Set oRegExpre = new RegExp
oRegExpre.Global = true
oRegExpre.Pattern = "{ img.*?}"

'iterate the array (records)
Set records = oRegExpre.Execute(res)
for each record in records
  aRecord = split(record,"'")
  key1 = replace(aRecord(0),"{ ","")
  key2 = replace(aRecord(2),", ","")
  wscript.echo key1 & aRecord(1) 'schow key & value pairs
  wscript.echo key2 & aRecord(3)
next

=>
img: source url1
thumb: source url1
img: source url2
thumb: source url2
img: source url3
thumb: source url3
于 2012-05-28T14:40:54.223 に答える