pilau
pilau

Reputation: 6723

ASP Classic: Iterating a json2.js object

I am getting a JSON result in ASP Classic like this:

<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)
%>

It works.

Let's say the JSON result will look like this:

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',
    ...
}

I want to then iterate the gallery object, not in JScript, but in VBScript.

How can I do that?

Thank you very much in advance.

Upvotes: 2

Views: 6370

Answers (3)

Efe
Efe

Reputation: 954

AXE(ASP Xtreme Evolution) JSON parser will do the trick for you. Just google it. Once you include AXE in your file;

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

edit---

When you will need to create a loop for the gallery;

dim key : for each key in Info.keys() Response.write( key & vbNewline ) next

Upvotes: 2

peter
peter

Reputation: 42207

If the JSON is simple and always the same structure you can parse it yourself, it's fast and simple an dno need for an external library.

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

Upvotes: 0

AnthonyWJones
AnthonyWJones

Reputation: 189477

If it is really necessary to enumerate an object's properties in VBScript then you will need to convert the object to a dictionary. Here is a simple function that will take a JScript object and return 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>

With this function present in your page you can now convert the gallery property to a dictionary:

 res.gallery = JScriptObjectToDictionary(res.gallery)

You can then iterate as:

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

Having said that its worth pointing out that properties named as a series like image1, image2 are a poor choice. It would be better if the JSON defined gallery as a simple array rather than an object. If you are in a position to influence the JSON design you should require that it be changed.

Upvotes: 2

Related Questions