Rreh
Rreh

Reputation: 33

Parsing JSon in Classic ASP with ASP Xtreme Evolution

I'm working on an classic ASP-project and I use ASP Xtreme Evolution to parse JSon data (found here: http://zend.lojcomm.com.br/entries/classic-asp-json-revisited/)

Anyway... I'm getting most of the JSon-data correctly but now I'm stuck on an Array.

The JSon looks like this:

{
  "Product": {
    "ID": "6666",
    "Name": "Tha name",
    "ArticleGroup": [
      {
        "@handleas": "array",
        "Title": "Title 1",
        "Article": {
          "ID": "777",
          "Label": "Label 1",
        }
      },
      {
        "@handleas": "array",
        "Title": "Title 2",
        "Article": {
          "ID": "888",
          "Label": "Label 2",
        }
      }
    ]
    }
  }
}

And the ASP look like this:

set xmlHTTP = server.createobject("MSXML2.ServerXMLHTTP.6.0")
xmlHTTP.open "GET", "http://source.of.json", false
xmlHTTP.send()
ProductFeed = xmlHTTP.ResponseText

dim ProductInfo : set ProductInfo = JSON.parse(join(array(ProductFeed)))

    dim key : for each key in ProductInfo.Product.keys()
        Response.Write ProductInfo.Product.ID ' Prints 6666
        Response.Write ProductInfo.Product.Name ' Prints Tha Name
    Next
    set ProductInfo = nothing

My problem is that I cannot figure out how to access the information in ArticleGroup. All I get is [object Object],[object Object] or an empty value.

Anyone have any ideas?

Thanx!

Upvotes: 3

Views: 5181

Answers (1)

Guido Gautier
Guido Gautier

Reputation: 1233

Try the following (based on the given JSON):

Dim ArticleGroup

dim key : For Each key in ProductInfo.Product.keys()
    Response.Write ProductInfo.Product.ID ' Prints 6666
    Response.Write ProductInfo.Product.Name ' Prints Tha Name            

    For Each ArticleGroup In ProductInfo.Product.Get("ArticleGroup") '' iterate all ArticleGroup entries
        Response.write ArticleGroup.Get("Title") '' get the correct key value
    Next
Next

You need to iterate all ArticleGroup entries and get the value through .get(keyName). Otherwise it will return the JScript function body instead.

Upvotes: 3

Related Questions