Hi everybody! I'm currently working on three projects using AXE (ASP Xtreme Evolution) Framework and because of their high dependency on JSON I've revisited the topic Classic ASP JSON support. The great news about it is that I found Troy Forster JSON2.ASP a really promising way to work with it. Despite the work being incomplete in terms of functionality, it used a really elegant way to READ the JSON in a fancy native looking way. Plus, the library was based on the Douglas Crockford json2.js
meaning it's engine is really strict to the standards.
Because of the AXE philosophy of embrace and use the great ideas born around the world in a real collaboration environment of all languages, I felt really tempted to adopt the original work of the JSON author himself and augment the AXE Framework functionality with this little piece of gold. But I couldn't replace, also it wasn't a smart move in terms of compatibility, my old full featured JSON.ASP class with other that would restrict the freedom to manipulate the Javascript object by my own will.
And that's why I coded my own version of JSON2.ASP which instantly became an integrated piece of AXE. It provides all the functionalities from the Troy Forster work but goes beyond enabling developers to augment the object with booleans, numbers, strings, arrays (using ASP safeArrays notation) and even another objects. Plus I implemented akeys
method in the Object.prototype
which allows the enumeration of the object keys (this modification is fine and it's also standard in ECMAScript 5) which means that it doesn't matter in which language you are programming (Ruby, Python, VBScript etc) you can use the for each
loop in same way that it's available for the language in ASP.
Here are some examples of how to use it:
Reading data from JSON
<%
dim Info : set Info = JSON.parse(join(array( _
"{", _
" ""firstname"": ""Fabio"",", _
" ""lastname"": ""Nagao"",", _
" ""alive"": true,", _
" ""age"": 27,", _
" ""nickname"": ""nagaozen"",", _
" ""fruits"": [", _
" ""banana"",", _
" ""orange"",", _
" ""apple"",", _
" ""papaya"",", _
" ""pineapple""", _
" ],", _
" ""complex"": {", _
" ""real"": 1,", _
" ""imaginary"": 2", _
" }", _
"}" _
)))
Response.write(Info.firstname & vbNewline) ' prints Fabio
Response.write(Info.alive & vbNewline) ' prints True
Response.write(Info.age & vbNewline) ' prints 27
Response.write(Info.fruits.get(0) & vbNewline) ' prints banana
Response.write(Info.fruits.get(1) & vbNewline) ' prints orange
Response.write(Info.complex.real & vbNewline) ' prints 1
Response.write(Info.complex.imaginary & vbNewline) ' prints 2
' You can also enumerate object properties ...
dim key : for each key in Info.keys()
Response.write( key & vbNewline )
next
' which prints:
' firstname
' lastname
' alive
' age
' nickname
' fruits
' complex
set Info = nothing
%>
Building a JSON
<%
dim Info : set Info = JSON.parse("{""firstname"":""Fabio"", ""lastname"":""Nagao""}")
Info.set "alive", true
Info.set "age", 27
Info.set "nickname", "nagaozen"
Info.set "fruits", array("banana","orange","apple","papaya","pineapple")
Info.set "complex", JSON.parse("{""real"":1, ""imaginary"":1}")
Response.write( JSON.stringify(Info, null, 2) & vbNewline ) ' prints the text below:
'{
' "firstname": "Fabio",
' "lastname": "Nagao",
' "alive": true,
' "age": 27,
' "nickname": "nagaozen",
' "fruits": [
' "banana",
' "orange",
' "apple",
' "papaya",
' "pineapple"
' ],
' "complex": {
' "real": 1,
' "imaginary": 1
' }
'}
set Info = nothing
%>
Download & Source
- Download/Source JSON2.ASP.
- Check the tests: http://labs.evolved.com.br/unittests/json2.unit.test.asp
Total Number of Words: 503
7 Comments
- Rohit Desai30 Sep 10 at 5:05am
Thanks for the post. Exactly what i was looking for.
Thanks a ton
- ozzie3 Jan 11 at 3:36am
hi, i really love your work, thx for that!! but here’s a question:
i want to create an array of objects like this:dim arr(10)
arr(0) = "{""id1"": 1, ""name"": ""aaa""}"
arr(1) = "{""id2"": 2, ""name"": ""bbb""}"
arr(2) = "{""id3"": 3, ""name"": ""ccc""}"
...but when i set it up like this:
Info.set "dummy_array", arri get this:
"dummy_array2": [
"{\"id1\": 1, \"name\": \"aaa\"}",
"{\"id2\": 2, \"name\": \"bbb\"}",
"{\"id3\": 3, \"name\": \"ccc\"}",
...can you help me get this done?
thx, ozziedim arr(10)
set arr(0) = JSON.parse("{""id1"": 1, ""name"": ""aaa""}")
set arr(0) = JSON.parse("{""id2"": 2, ""name"": ""bbb""}")
set arr(0) = JSON.parse("{""id2"": 2, ""name"": ""ccc""}")
...
- Yasin2 May 11 at 0:12am
Hi,
How can we use “For each”? I'm getting the error below:
Regards.URL = "hotmail"
TweetUrl = "http://search.twitter.com/search.json?q=" & URL
strJSON = get_page_contents(TweetUrl)
Set tweet = JSON.parse(strJSON)
For each tw in tweet.results
i = 0
Response.Write tweet.results.[i].text & "<br>"
Nextfor each result in tweet.results.keys()
Response.write( tweet.results.get(result) )
Response.write( "<br />" )
next
- rob12 Aug 11 at 0:42am
I am trying to figure out how I can handle a json post from a 3rd party. They are sending me a json post with 3 variables to a url I host. I need to use those 3 variables in a SQLstored procedure that I call using a classic ASP page.
I currently have everything working fine using standard Querystring Values that were posted to my classic ASPpage. I need some help with getting this process to work with json values that are posted to the url instead of standard Querystring values. They are sending me the values in json and awaiting a reply. I think I need a way to get the json values, dim them, and be able to use them in my classic asp SQL stored procedure call. Then the Stored Procedure Return Value can be the reply that their json reply is pending.
Any assistance would be great on how I can accomplish this.
- Jamie14 Dec 11 at 1:35pm
I’m trying to import Tweets in the same way as Yasmin above.
dim tweet : set tweet = JSON.parse(thejsonfile)
for each result in tweet.results.keys()
Response.write( tweet.results.get(result) )
Response.write("<br />")
next
set tweet = nothingBut the for each loop is returning:
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]How do I access the variables of each tweet (ie created_at, profile_image_url, to_user_name, etc)? Your help would be much appreciated.
- BP10 Feb 12 at 10:51am
Hello, I maybe a little, or maybe missing something here.
But how would a loop through these?[
{
"category": "burglary",
"month": "2011-12",
"location": {
"latitude": "52.0831588",
"street": {
"id": 1136972,
"name": "On or near Fastnet Close"
},
"longitude": "0.45835896"
},
"context": "",
"id": 8907706,
"location_type": "Force"
}, {
"category": "other-theft",
"month": "2011-12",
"location": {
"latitude": "52.0877479",
"street": {
"id": 1136982,
"name": "On or near Wratting Road"
},
"longitude": "0.44091859"
},
"context": "",
"id": 8907763,
"location_type": "Force"
}
]dim source, Collection, Item, index
source = <your_code_goes_here>
set Collection = JSON.parse(source)
for each index in Collection.keys()
set Item = Collection.get(index)
Response.write( Item.category )' and so goes on
set Item = nothing
next
set Collection = nothing
- Peter25 Apr 12 at 5:07am
hoping you can help me out,
I want to post the amount of likes
from facebook.json link
i’ve been running around in circles over a month already but every solution i try throws me
Microsoft JScript runtime error ‘800a139e’
Exception thrown and not caught json2.asp, line 782basicly my index.asp is the following
example 1:
<!— #include file=“json2.asp” —>
<%
Dim source,Collection,Item,indexsource = “https://graph.facebook.com/19292868552”
set Collection = JSON.parse(source)
for each index in Collection.keys() set Item = Collection.get(index) Response.write( Item.likes ) set Item = nothing
next
set Collection = nothing%>
example 2:
<!—#include file=“json2.asp”—>
<%Dim facebook
Set facebook =JSON.parse(”{https://graph.facebook.com/19292868552}”) Response.Write(“brand: “ & facebook.name & “<br/>”)
Response.Write(“model: “ & facebook.website & “<br/>”)
Response.Write(“new Json: “ & JSON.stringify(facebook) & “<br/>”) Set facebook = Nothing
%>
'프로그램&DB > ASP' 카테고리의 다른 글
ASP에서의 DB값 NULL체크 하기 by 뀨유뀨유님 (0) | 2013.10.01 |
---|---|
ASP - Transaction (트랜잭션) 구현 by 김지민님 (0) | 2012.09.27 |
ASP 성능향상을 위한 15계명 (0) | 2012.05.10 |
Request Object - ServerVariables Collection by Taeyo(김 태영)님 (0) | 2012.02.15 |
ASP 내장 함수 모음 (0) | 2011.11.25 |