kneidels
kneidels

Reputation: 914

Parse a JSON object using ASPjson

In Classic ASP (VBScript) I can do a general request of POST using request.form or GET using request.querystring which would give me the entire string that was sent.

But, I now need to receive a JSON object from a client side location. This is an example of what it might look like:

{
  "firstName": "John",
  "lastName" : "Smith",
  "age"      : 25
}

How do I request this entire object (which I will then be parsing with ASP.JSON)?

PS: I know that I can probably convert the JSON object to a string on the client side, and then parse as text on the server side, but that feels like a work-around rather than a straight solution.

Upvotes: 1

Views: 1336

Answers (1)

luiscabus
luiscabus

Reputation: 309

First of all, I wouldn't use that AspJson, but this: https://github.com/rcdmk/aspJSON

Second, you're not receiving an object per se, but request that contains a "string version of the json object". In this case, probably bytes, thats why you're going to BinaryRead and convert it into a body first.

You then will be able to parse the body with any parser you want.

Now let's try to give you an example code:

<%Response.LCID = 1033%>
<!--#include file="__jsonObject.class.v3.8.1.asp" -->

Set UTF8Enc = CreateObject("System.Text.UTF8Encoding") ' .NET COMPONENT, required on the server app pool
Set JSON = new JSONobject

lngBytesCount = Request.TotalBytes
request_body = UTF8Enc.GetString(Request.BinaryRead(lngBytesCount))

Set request_json = JSON.parse(request_body)

first_name = request_json("firstName")
last_name = request_json("lastName")
age = request_json("age")

Upvotes: 2

Related Questions