Can´t figure out how to solve this with ASP Classic, an external server will send me some Json to my site ex. www.mypage.com/getjson.asp
{"Userid":"112233","Member":Tom}
How can I fetch this and check if the Userid already exist, otherwise put it in the SQL
If the userid exist I just have to answer
response.Write "{""Userid"": true}"
Can I "convert" it to a querystring or somthing ?
Edit: Thanks for all help, but I coundn´t get it to work with the tips in this thread But I managed to get the whole string whit this code
bytecount = Request.TotalBytes
bytes = Request.BinaryRead(bytecount)
Set stream = Server.CreateObject("ADODB.Stream")
stream.Type = 1
stream.Open()
stream.Write(bytes)
stream.Position = 0
stream.Type = 2
stream.Charset = "utf-8"
s = stream.ReadText()
stream.Close()
Set stream = nothing
Response.Write =(s)
But now I need to figure put how to cleanup and split this, note that Member don´t have qoutes
{"Userid":"00004547552009","Member":1,"id":"0060a80040d9"}
Mate, i suggest you to check the ASP JSON Class, its make your code clean and so easy to code. Check: http://www.aspjson.com/
Cheers.
Ok long time later … only a partial answer addressing asp parsing the URL query containing JSON. Once that happens, the SQL becomes easy.
This answer uses Jquery - it hinges on how Jquery converts the JSON before sending (via .get).
I'm posting this because a simpler version of John's already simple answer works for me. It's just key-value URL pairs!
And - caveat - although John's example works with some modifications, making the libraries mentioned unnecessary, I cannot vouch that this approach will work for more complex JSON - I haven't tried. (Also I'm glad he mentions mixing server-side jscript with asp's vbscript - a useful technique I'd forgotten about.)
Client-side code (swiped and modded from W3schools JSON / Ajax)
Note use of .get not .post:
( Properly speaking, UserID, Name, and City should be wrapped in quotes, but either way parses correctly in this example. )
And here's the server-side code (Based on John's answer):
And what about use my utility class: https://github.com/rcdmk/aspJSON
Some examples from the README:
Edit:
I've added methods to load recordsets and bidimensional arrays some time ago. This can help a lot in writing JSON from the database:
You really need a JSON passer for this. Best one I have found is Extreme JSON A VbsJson class which has a "Decode" method to parse JSON to VBScript and a "Encode" method to generate JSON from VBScript. The code is somewhat long, I have extracted the Encode and Decode functionality and have used it. It seesm to work well.
I get the feeling you just have this simple little piece of JSON which is always the same. With different values of course but always with a userID and a member. If so here is something that will work for you. note - You are missing quotes around "Tom" in your example. Save this to a txt file called json.txt in the same dir as your .asp page:
Now put this in your page. It should be cut n paste except for it expects you have a Database connection object called conn. Oh and you will have to change the query
UPDATE - As I have said the solution above is customised for a JSON format that you know will not change, is simple and you know the one integer value is the one you are looking for. You should use a JSON paser for anything else!
You say your not sure about how JSON is sent. Its a text string that will be sent and available to you via the request object. Either .form or .querystring. I always avoid querystring if I can in all situations. The form collection is better for many reasons (another whole conversation). No someone can not pass you JSON like your example. I believe it must be encoded E.G:
Would look like this:
Classic ASP supports Javascript as well as VBScript, and in my experience its much easier to use it to deal with JSON than to use any of the JSON VBS classes which are out there. You can declare Javascript as your language at the top of the page (
<%@language="javascript"%>
) but if you prefer to use VBS elsewhere you could use something like this in the head section of your page. Note the runat="Server" attributethe strings
jUserId
andjMember
would then be available for you to use elsewhere in your page within VBS code inside <% %> delimiters and you can use them in your database insert.