2

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.

pilau
  • 6,635
  • 4
  • 56
  • 69
  • 1
    related http://stackoverflow.com/questions/1019223/any-good-libraries-for-parsing-json-in-classic-asp –  May 28 '12 at 11:07

3 Answers3

2

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

Efe
  • 944
  • 3
  • 19
  • 37
2

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.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
  • Yes, it is possible to change that to an array. Would you mind updating your answer with another snippet according to that? Thank you. – pilau May 28 '12 at 16:02
  • Why an array? In this case a dictionary is sufficient. Arrays have a fixed dimension, therefor appending an item to an array is inefficient in VBScript. – Guido Gautier May 30 '12 at 15:19
  • Well the JSON "gallery" will have to be implemented as an array anyway, like Anthony says, and then in VBScript it will have to be parsed into a dictionary, like you say Guido. I don't have any idea how to do that though, and would appreciate a code example a lot :) Bottom line is I need to iterate this object in VBScript, not JScript. Thanks. – pilau May 30 '12 at 17:49
  • @Pilau: Do you need a VBScript array or is a JScript array sufficient (you can foreach items in a JScript array). To get a JScript array change the JSON to something sensable. If you want a VBScript array is the order of items in the array important? – AnthonyWJones May 31 '12 at 09:01
  • I need a VBScript array, thanks - and the order of the items in the gallery is not important. I will just have have to iterate over this list of items in VBScript eventually. – pilau May 31 '12 at 13:09
0

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
peter
  • 41,770
  • 5
  • 64
  • 108