How to access JSON data in classic ASP - json

I'm working on an classic ASP-project and I use ASP Xtreme Evolution to parse JSon data (found here: How to access JSON data in classic ASP using json2.asp or aspjson libraries?)
I tried, but I can't. how do i access this json data?
{
"result":[
{
"key":"2607037",
"sts":"finished",
"stsdetail":"Finished",
"sname":"Ice Hockey",
"home":{
"1p":"0",
"fe":"2",
"3p":"1",
"2p":"1",
"ord":"2",
"rs":"2"
},
"away":{
"ord":"0",
"1p":"0",
"rs":"0",
"2p":"0",
"3p":"0",
"fe":"0"
}
},
{
"key":"2435948",
"sts":"finished",
"stsdetail":"Finished",
"sname":"Soccer",
"home":{
"ord":"2",
"rs":"2",
"fe":"2",
"ht":"1"
},
"away":{
"ord":"0",
"rs":"0",
"fe":"0",
"ht":"0"
}
},
{
"key":"2606031",
"sts":"cancelled",
"stsdetail":"Cancelled",
"sname":"Tennis"
}
]
}
code:
set restaurant = oJSON.data("result")
for each itm in restaurant
if Not IsObject(restaurant.item(itm)) then
Response.write itm &" : "& restaurant.item(itm) & "<br/>"
else
'opening
for each dayy in restaurant.item(itm)
Response.write dayy & ":"
Response.write restaurant.item(itm)(dayy)
If restaurant.item(itm)(dayy) <> "" Then
Response.write " - "
Response.write restaurant.item(itm)(dayy)
End If
Response.write "<br/>"
next
end if

you're missing the indexes in the array, try this:
for each dayy in restaurant.item(itm)
Response.write dayy & ":"
Response.write restaurant.item(itm)(dayy)(0)
If restaurant.item(itm)(dayy)(1) <> "" Then
Response.write " - "
Response.write restaurant.item(itm)(dayy)(1)
End If
Response.write "<br/>"
next

Related

Looping through JSON Array using ASPJSON

Thanks to advice from here (Looping through JSON using ASPJSON) and here (ASP JSON: Object not a collection), I am starting with a JSON array in Classic ASP:
{"markers": [{
"event":"hard_bounce",
"msg":{
"ts":1365109999,
"subject":"This an example webhook message",
"email":"example.webhook#mandrillapp.com",
"sender":"example.sender#mandrillapp.com",
"tags":[
"webhook-example"
],
"state":"bounced",
"metadata":{
"user_id":111
},
"_id":"exampleaaaaaaaaaaaaaaaaaaaaaaaaa",
"_version":"exampleaaaaaaaaaaaaaaa",
"bounce_description":"bad_mailbox",
"bgtools_code":10,
"diag":"smtp;550 5.1.1 The email account that you tried to reach does not exist. Please try double-checking the recipient's email address for typos or unnecessary spaces."
},
"_id":"exampleaaaaaaaaaaaaaaaaaaaaaaaaa",
"ts":1433940242
},
{
"event":"hard_bounce",
"msg":{
"ts":1365109999,
"subject":"This an example webhook message",
"email":"example.webhook#mandrillapp.com",
"sender":"example.sender#mandrillapp.com",
"tags":[
"webhook-example"
],
"state":"bounced",
"metadata":{
"user_id":111
},
"_id":"exampleaaaaaaaaaaaaaaaaaaaaaaaaa1",
"_version":"exampleaaaaaaaaaaaaaaa",
"bounce_description":"bad_mailbox",
"bgtools_code":10,
"diag":"smtp;550 5.1.1 The email account that you tried to reach does not exist. Please try double-checking the recipient's email address for typos or unnecessary spaces."
},
"_id":"exampleaaaaaaaaaaaaaaaaaaaaaaaaa1",
"ts":1433940242
}]
}
I am using Classic ASP and ASPJSON (http://www.aspjson.com/).
I can access the "_id" and "ts" values from the loop via:
Set oJSON = New aspJSON
'read the local JSON data
oJSON.loadJSON(str2)
For Each thingy In oJSON.data("markers")
Set this = oJSON.data("markers").item(thingy)
Response.Write _
this.item("_id") & ": " & _
this.item("ts") & "<br>"
Next
However, I am stuck trying to get at the data one level down, in the "msg" section.
I did try:
For Each thingy In oJSON.data("markers")
Set this = oJSON.data("markers").item(thingy)
Response.Write _
this.item("_id") & ": " & _
this.item("ts") & "<br>"
For Each thingy2 In oJSON.data("msg")
Set this2 = oJSON.data("this2").item(thingy2)
Response.Write _
this2.item("subject") & ": " & _
this2.item("diag") & "<br>"
Next
Next
But get this error:
Description: Object not a collection : .
Relating to this line:
For Each thingy2 In oJSON.data("msg")
I guess I am doing something silly, but I am stuck on this one and can't work out how to access that data.
You could access msg, which is a json object, within the loop you already have:
For Each thingy In oJSON.data("markers")
Set this = oJSON.data("markers").item(thingy)
Response.Write _
this.item("_id") & ": " & _
this.item("ts") & "<br>" &_
this.item("msg").item("subject") & ": " &_
this.item("msg").item("diag") & "<br>"
Next

Parsing json trying to get value

I am trying to get the value "aaG40a2f-53d3-8f74-3c200403223" from the json code below that's in json.
My vb.net code looks like this:
For Each Row In json("data")("records")("Form")
Try
For rowNum = 0 To result_RowNames.Length - 1
result_rowData(intX) = NullSafeSelect(Row, result_RowNames(rowNum))
Select Case result_RowProperTypes(rowNum)
Case "textField"
_tmpHtml += "<td>" & vbCrLf
_tmpHtml += "<input type=""text"" " & _
"data-first=""yes"" " & _
"data-uid=""" & NullSafeSelect(Row("#uid"), result_RowNames(rowNum)) & """ " & _
"onchange=""updateChange(this);"" " & _
"id=""textbox_" & rowNum & """ " & _
"class=""form-control small"" " & _
"value=""" & result_rowData(intX) & """>" & vbCrLf
......etc etc...
My json in ("data")("records")("Form") looks like this:
{
"#application_uid": "af74c279-4340-8441-2dR4e696gb1",
"#draft_ownerid": "",
"#flowState": "ST_NewName",
"#has_draft": "",
"#id": "63",
"#uid": "aaG40a2f-53d3-8f74-3c200403223",
"F_FName": "test1",
"F_LName": "test2"
}
The code above works - it just does not produce a value for uid as seen below in the output of HTML:
<input type="text" data-first="yes" data-uid="" onchange="updateChange(this);" id="textbox_0" class="form-control small" value="test1">
Looks like all I needed to do was just to use
Dim tmpUID = Row("#uid").ToString
in order to get the value I was looking for.

Trying to resolve an ASP Classic Script revolving around two buttons. It contains two buttons when only needing one

here is the ASP Script and the html elements inside of the body tags. For some reason it is giving me TWO buttons instead of one. My Goal is just trying to remove one of the buttons so it can lead me into the View cart feature page. Thank you for the help!
<%
catid = Request.QueryString("id")
sub productInfo(connObj,category)
sqlCustomer = "SELECT * FROM qryProdsCategory WHERE ccategory = '" & Cint(category) & "'"
Set rs = Server.CreateObject ("adodb.Recordset")
rs.Open sqlCustomer, dbc, adOpenDynamic, adLockOptimistic, adCmdText
if not rs.EOF then
if Session("sort")="0" then
rs.sort = "cname ASC"
end if
Response.Write "<form action="&q&Application("secureurl")&"/cart/view-cart.asp"&q&" method="&q&"POST"&q&" name=form"&i&">"
while not rs.EOF
If rs("stock")="1" then
Response.Write "<a href=""product.asp?id=" & rs("catalogID") & ""
Response.Write "" & rs("catalogID") & "" & rs("manModNum") & "</font></td><td width=""18%"" rowspan=""2"">"
Response.Write "<input type="&q&"hidden"&q&" name="&q&"fproductid"&q&" value="&q & rs("catalogID")& q&">"
Response.Write "<input type="&q&"hidden"&q&" name="&q&"fquantity"&q&" value=1>"
Response.Write "<input type="&q&"hidden"&q&" name="&q&"fcat"&q&" value=" & rs("ccategory") & ">"
End If
rs.MoveNext
wend
If rs.RecordCount > 0 then
Response.Write "<button class=""btn"" TYPE=""btn"" style=""background-color: #cb0000;color: #fff;"">ADD TO CART</button> </form>"
End If
Response.Write "</form>"
else
Response.Write " <P><Center><font size=""2""><h3>Sorry, but products information for the category you have chosen is not available at this moment. Please check back soon!</H3></font></center>"
catname = "Error"
end if
end sub
%>
Here is the code inside the body tags
<div class="span3">
<img src="img/team/profile1.jpg">
<div class="productSelection">
<div class="ProductTitle">
<strong style="font-size:16px;">TITLE</strong>
</div>
<h6>TITLE</h6>
<h6>TITLE</h6>
<%
call openConn()
call productInfo(dbc,catid)
%>
This is the Edited part of the View Cart Section of the ASP Code
<%
catid = Request.QueryString("id")
sub productInfo(connObj,category)
sqlCustomer = "SELECT * FROM qryProdsCategory WHERE ccategory = '" & Cint(category) & "'"
Set rs = Server.CreateObject ("adodb.Recordset")
rs.Open sqlCustomer, dbc, adOpenDynamic, adLockOptimistic, adCmdText
if not rs.EOF then
if Session("sort")="0" then
rs.sort = "cname ASC"
end if
Response.Write "<form action="&q&Application("secureurl")&"/cart/view-cart.asp"&q&" method="&q&"POST"&q&" name=form"&i&">"
while not rs.EOF
If rs("stock")="1" then
Response.Write "<a href=""product.asp?id=" & rs("catalogID") & ""
Response.Write "" & rs("catalogID") & "" & rs("manModNum") & "</font></td><td width=""18%"" rowspan=""2"">"
Response.Write "<input type="&q&"hidden"&q&" name="&q&"fproductid"&q&" value="&q & rs("catalogID")& q&">"
Response.Write "<input type="&q&"hidden"&q&" name="&q&"fquantity"&q&" value=1>"
Response.Write "<input type="&q&"hidden"&q&" name="&q&"fcat"&q&" value=" & rs("ccategory") & ">"
End If
rs.MoveNext
wend
%>
You are getting two buttons because you are creating them within the While loop. The condition within the while loop may be satisfied couple of times (I mean your code goes to the Else part where you create the button all the time). Move the button creation logic out of the while loop. And, your If...Else code to create the button doesn't make sense. I.e. whatever the condition it creates a button. According to my understanding you are trying to show the Add to Cart button only if there are records. If that's true I suggest something like this.
<%
catid = Request.QueryString("id")
sub productInfo(connObj,category)
sqlCustomer = "SELECT * FROM qryProdsCategory WHERE ccategory = '" & Cint(category) & "'"
Set rs = Server.CreateObject ("adodb.Recordset")
rs.Open sqlCustomer, dbc, adOpenDynamic, adLockOptimistic, adCmdText
if not rs.EOF then
if Session("sort")="0" then
rs.sort = "cname ASC"
end if
Response.Write "<form action="&q&Application("secureurl")&"/cart/view-cart.asp"&q&" method="&q&"POST"&q&" name=form"&i&">"
while not rs.EOF
If rs("stock")="1" then
Response.Write "<a href=""product.asp?id=" & rs("catalogID") & ""
Response.Write "" & rs("catalogID") & "" & rs("manModNum") & "</font></td><td width=""18%"" rowspan=""2"">"
Response.Write "<input type="&q&"hidden"&q&" name="&q&"fproductid"&q&" value="&q & rs("catalogID")& q&">"
Response.Write "<input type="&q&"hidden"&q&" name="&q&"fquantity"&q&" value=1>"
Response.Write "<input type="&q&"hidden"&q&" name="&q&"fcat"&q&" value=" & rs("ccategory") & ">"
End If
rs.MoveNext
wend
If rs.RecordCount > 0 then
Response.Write "<button class=""btn"">ADD TO CART</button>"
End If
Response.Write "</form>"
else
Response.Write " <P><Center><font size=""2""><h3>Sorry, but products information for the category you have chosen is not available at this moment. Please check back soon!</H3></font></center>"
catname = "Error"
end if
end sub
%>
Hope this helped! Vote and accept the answer if it did. All the best :-)

Post HTML form data to CSV and create email notification using ASP

unfortunately I am forced to use ASP for this particular task. I need my form to post its data to a CSV and then send an email notifying the client. Now the first bit I already have working (posting the data to a CSV file) but its the email notification that is getting me, as I have no idea how or what to incorporate into my current code in order for this to happen.
Here is a link to the form in question:
http://dev.brandspank.co.za/form/index.html
And below is my ASP code:
<% option explicit
dim objFSO, objTXT, lines, newRecord, filePath, fieldNames, x, fullText, nLine %>
<h1>Form inputs posted:</h1>
<%
for each x in request.form
response.write x & ": " & Replace(request.form(x), ",","-") & "<br />" & vbNewLine
next
'set filepath for plain text db. This neds to be the absolute path of file
filePath = "\form\myCSVdb.csv"
set objFSO = server.createobject("Scripting.FileSystemObject")
if (objFSO.fileExists(filePath))=true then
set objTXT = objFSO.openTextFile(filePath, 1) 'opens a text file for
' reading, true means it will create the file if not already there
fullText = trim(objTXT.readall)
lines = split(fullText, vbNewLine) 'lines is now an array, each item is
' one line of the db file.this could now be used to list the entire db
' table, notice next 3 commented out lines
' for each x in lines
' response.write lines(x)
' next
objTXT.close
set objTXT = nothing
if trim(lines(0)) = "" then fullText = ""
else
fullText = ""
end if %>
<h1>added to the db:</h1>
<%
if fullText <> "" then 'there are already field names in the db,
' so put the new line in the same order
set objTXT = objFSO.openTextFile(filePath, 8, True) 'opens the text file for
' appending
response.write "(fields in the database: " & lines(0) & ")<br />" & vbNewLine
'split the first line, which had field names into an array -fieldNames-
fieldNames = split(lines(0), ",")
response.write "field values entered:<br />" & vbNewLine
for each x in fieldNames
if x <> "" then
nLine = nLine & Replace(request.form(x), ",","-") & ","
'adds each form input to a string
response.write x & ": " & Replace(request.form(x), ",","-") & "<br />" & vbNewLine
end if
next
nLine = left(nLine, len(nLine)-1)
'removes trailing comma
objTXT.writeLine nLine
else 'there isn't anything in the textfile yet, so put in the field names first
set objTXT = objFSO.openTextFile(filePath, 2, True) 'opens the text file for
' writing
response.write "field names enterd:<br />" & vbNewLine
for each x in request.form
if Replace(lcase(x), ","," ") <> "submit" then 'or you will have a "submit" field in your db
'of course, if your submit button is named something else, that should
'be the name xcluded here
nLine = nLine & x & ","
'adds each form input name to a string which will become th first line of
'the db, the line which shows field names
response.write x & "<br />" & vbNewLine
end if
next
nLine = left(nLine, len(nLine)-1)
'remove trailing comma
objTXT.write nLine
objTXT.write vbNewLine
nLine = ""
response.write "field values entered:<br />" & vbNewLine
for each x in request.form
if lcase(x) <> "submit" then
nLine = nLine & Replace(request.form(x), ",","-") & ","
'adds each form input to a string
response.write Replace(request.form(x), ",","-") & "<br />" & vbNewLine
end if
next
nLine = left(nLine, len(nLine)-1)
'remove trailing comma
objTXT.write nLine
objTXT.write vbNewLine
end if
objTXT.close
%>
<% response.redirect "thankyou.html" %>
Any help would be very much appreciated!
I put pretty much everything you need here:
www.oceanmedia.net/files/2014-06-form-process.zip
Included:
form_process. asp
i_fn_email_cdo.asp
i_check_security.asp
i_odbc.asp
i_fn_clean.asp
i_fn_dirty.asp
Oh and the text file part:
<%
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
'Open the file for reading
Set f = fso.CreateTextFile(s_path & "/" & s_file_being_created, True)
f.Write(m)
f.Close
Set f = Nothing
Set fso = Nothing
%>
Sorry, I don't yet have comment privileges, so this will be a comment disguised as an answer: the specifics will depend upon how your web server is configured in terms of email, but assuming it is somewhat current, you will probably be using CDOSYS to do so, and here is a link to a very basic intro: http://www.w3schools.com/asp/asp_send_email.asp
You've navigated the FileSystemObject, so you'll probably find this fairly straightforward once you get the details right on your mail server.

ASP Classic code returning true instead of response.write result

I have a piece of ASP Classic code that instead of response writing my information it returns "true"
Here is the relevant line:
response.write "<a href='search.htm?supplier_name= & request.querystring('supplier_name') & "&aircraft_type=" & request.querystring('aircraft_type') & "&state=" & request.querystring('state') & "&order=state'>State</a>"
I believe the problem is associated with this section:
& "&order=state'>State</a>"
The ASP code in full:
<%
if request.querystring("order") = "state" then
response.write("State")
else
response.write "<a href='search.htm?supplier_name= & request.querystring('supplier_name') & "&aircraft_type=" & request.querystring('aircraft_type') & "&state=" & request.querystring('state') & "&order=state'>State</a>"
end if
%>
Missing a double quote after supplier_name=:
response.write "<a href='search.htm?supplier_name=" & request.querystring('supplier_name') & "&aircraft_type=" & request.querystring('aircraft_type') & "&state=" & request.querystring('state') & "&order=state'>State</a>"