http://tforster.wik.is/ASP_Classic_Practices_For_The_21st_Century/JSON4ASP


sample

<!--#include file="JSON_latest.asp"-->
<%
Dim member
Set member = jsObject()

member
("name") = "Tuğrul"
member
("surname") = "Topuz"
member
("message") = "Hello World"

member
.Flush
%>

output

{"name":"Tu\u011Frul","surname":"Topuz","message":"Hello World"}

SQL Queries

sample

<!--#include file="JSON_latest.asp"-->
<!--#include file="JSON_UTIL_latest.asp"-->
<%
QueryToJSON(dbconn, "SELECT name, surname FROM members WHERE age < 30").Flush
%>

output

[
   
{
       
"name":"ali",
       
"surname":"osman"
   
},
   
{
       
"name":"mahmut",
       
"surname":"\u00E7\u0131nar"
   
}
]

Multi Dimensional Arrays

sample

<!--#include file="JSON_latest.asp"-->
<%
Dim a(1,1)

a
(0,0) = "zero - zero"
a
(0,1) = "zero - one"
a
(1,0) = "one - zero"
a
(1,1) = "one - one"

Response.Write toJSON(a)
%>

output

[["zero - zero","zero - one"],["one - zero","one - one"]]
<script language="javascript" runat="server" src="json2.min.asp"></script>
<script language="vbscript" runat="server">
   Option Explicit
   
   Dim car: Set car = JSON.parse("{""brand"":""subaru"",""model"":""outback sport"",""year"":2003," & _
                                 """colour"":""green"",""accessories"":[" & _
                                 "{""foglamps"":true},{""abs"":true},{""heatedSeats"":true}]}")
                                 
   Response.Write("brand: " & car.brand & "<br/>")                               
   Response.Write("model: " & car.model & "<br/>")                               
   Response.Write("colour: " & car.colour & "<br/>")                               
   Response.Write("has foglamps: " & CStr(car.accessories.get(0).foglamps) & "<br/>")                               
   car.accessories.get(0).foglamps = false
   Response.Write("has foglamps: " & CStr(car.accessories.get(0).foglamps) & "<br/>")                               
   Response.Write("new Json: " & JSON.stringify(car) & "<br/>")
   
   Set car = Nothing
</script>

+ Recent posts