Reputation: 183
So I have a request to a JSON api working using client-side jquery and AJAX. Unfortunately, another api insists this be done from the server because of security concerns regarding the token. I'm a bit stumped. To begin, what I got working from client-side JavaScript looks like:
$.getJSON("https://api.xyz.com/mytoken/specificargument",function(data){...})
The function then fills data with JSON fields (or are they called properties?) as data.field1, data.field2, etc. These are then placed into their appropriate inputs with jquery code that lives inside the {...}. It's very straightforward.
I have read that this (i.e., obtaining the JSON string, not parsing it) can be done on the server with
Set objJSON = jsObject()
objJSON("apikey") = "mytoken"
Set objJSON("arg1") = jsObject()
objJSON("arg1")("arg1") = "specificargument"
set objXMLhttp = Sever.Createobject("MSXML2.ServerXMLHTTP")
objXMLhttp.open "POST","https://api.abc.com", false
objXMLhttp.setRequestHeader "Content-type","application/json"
objXMLhttp.setRequestHeader "Accept","application/json"
objXMLhttp.send objJSON.jsString
strResponse = objXMLhttp.responseText
Set objXMLhttp = Nothing
So that's pretty useful, except the posting says I need to use "appropriate declarations, includes, etc." So I thought it would be simple to find these, but querying for classic ASP JSON yields very confusing results. Can someone help with these:
Thank you.
Upvotes: 0
Views: 899
Reputation: 183
From the original poster: Typically, my situation may not generalize well, but because I found a very simple solution, I'll post it here in case it helps anybody else finding this.
First of all, for this particular api, the token and other argument were passed in the querystring and the method was GET, not POST. Thus the .send command had no argument. That's all it took to get the JSON string returned. Thus:
set objXMLhttp = Server.Createobject("MSXML2.ServerXMLHTTP")
objXMLhttp.open "GET","https://api.abc.com/argument1/?token=mysecret", false
objXMLhttp.setRequestHeader "Content-type","application/json"
objXMLhttp.setRequestHeader "Accept","application/json"
objXMLhttp.send
strResponse = objXMLhttp.responseText
By the way, if I HAD needed to send a JSON-formatted argument, as has been pointed out by others, often (for simple structures) it is easy to simply create this instead of using a 3rd-party library. (E.g., from a post on this site: {"Email":"[email protected]", "firstname":"joe", "lastname":"smith"} ).
Finally, just as it is easy to create simple JSON strings, returned data, if simple, can easily be parsed. In my case, the return always had a brace at the beginning and end, then was a simple JSON comma-delimited list of colon-pairs. This was easily split into a vbscript array (remember, my post was for classic ASP) and parsed into a dictionary (ParseJSON) as follows (note I needed to strip off all the quotation marks):
strResponse = Left(strResponse,Len(strResponse)-1) ' remove right-brace
strResponse = Right(strResponse,Len(strResponse)-1)
Dim ParseJSON
Set ParseJSON = CreateObject("Scripting.Dictionary")
ParseJSON.CompareMode = vbTextCompare
ArryJSON = Split(strResponse,",")
' parse into a dictionary
For Each jPair In ArryJSON
' take jPair apart and insert into dictionary
onePair = Split(jPair,":")
aa = onePair(0)
aa = Left(aa,Len(aa) - 1) ' remove quotation mark
aa = Right(aa,Len(aa) - 1)
bb = onePair(1)
bb = Left(bb,Len(bb) - 1)
bb = Right(bb,Len(bb) - 1)
ParseJSON.Add aa,bb
Next
Upvotes: 0
Reputation: 16682
The example code you have posted appears to be using the aspjson - JSON serializer for VBScript based ASP server technology library. One key thing in that statement is "JSON Serializer", it will only serialise from object to string, not the other way around which makes it kind of useless for most scenarios.
This is purely my opinion but a well-maintained library that will do both is JSON object class By RCDMK. The examples on the GitHub page are pretty simple to follow, but there are some examples of its usage on this very site.
Here is a quick example of its usage inside an ASP page;
<% Option Explicit %>
<!-- #include virtual="/scripts/jsonObject.class.asp" -->
<%
Dim JSON, parsedJSON
Dim jsonString: jsonString = "[{ ""strings"" : ""valorTexto"", ""numbers"": 123.456, ""arrays"": [1, ""2"", 3.4, [5, 6, [7, 8]]], ""objects"": { ""prop1"": ""outroTexto"", ""prop2"": [ { ""id"": 1, ""name"": ""item1"" }, { ""id"": 2, ""name"": ""item2"", ""teste"": { ""maisum"": [1, 2, 3] } } ] } }]"
Call init()
Sub init()
Response.LCID = 2057
Set JSON = New JSONobject
Call Response.Write("<h2>JSON String</h2>")
Call Response.Write("<pre>" & jsonString & "</pre>")
Call Test_ParseJSON()
Call Test_WriteJSON()
End Sub
Sub Test_ParseJSON()
Call Response.Write("<h2>Parsing JSON</h2>")
Set parsedJSON = JSON.Parse(jsonString)
Call Response.Write("<pre>parsedJSON(0).Value(""objects"").Value(""prop1"") = """ & parsedJSON(0).Value("objects").Value("prop1") & """</pre>")
End Sub
Sub Test_WriteJSON()
Call Response.Write("<h2>Writing JSON</h2>")
Call parsedJSON(0).Value("objects").Change("prop1", "hello test")
Call Response.Write("<pre>" & parsedJSON.Serialize() & "</pre>")
End Sub
%>
Outputs:
[{ "strings" : "valorTexto", "numbers": 123.456, "arrays": [1, "2", 3.4, [5, 6, [7, 8]]], "objects": { "prop1": "outroTexto", "prop2": [ { "id": 1, "name": "item1" }, { "id": 2, "name": "item2", "teste": { "maisum": [1, 2, 3] } } ] } }]
parsedJSON(0).Value("objects").Value("prop1") = "outroTexto"
[{"strings":"valorTexto","numbers":123.456,"arrays":[1,"2",3.4,[5,6,[7,8]]],"objects":{"prop1":"hello test","prop2":[{"id":1,"name":"item1"},{"id":2,"name":"item2","teste":{"maisum":[1,2,3]}}]}}]
Upvotes: 2