Reputation: 24488
What is the best way to parse json string into classic asp using a library?
Dim jsonString
jsonString = {"date":"4/28/2017","custType":"100","vehicle":"1"}
Would like to have
response.write("<li> date :" & json("date") & "</li>")
Upvotes: 4
Views: 25946
Reputation: 30520
Almost certain no-one will ever need this answer, but just in case.
Rather than rely on external libraries, or ScriptControl
objects, simply do it with some Server-Side JavaScript.
Set jsreader = New JsonReader
jsreader.loadJson(myJson)
adviser_id = jsreader.getElement("someField")
Using this VBScript module:
<%
class JsonReader
private root
private sub Class_Terminate()
set root = nothing
end sub
public sub loadJson(sJson)
set root = new_JsonEngine(sJson)
if root.hasError() then err.Raise 10000, "JsonReader:loadJson", "Syntax Error"
end sub
public function getElement(sPath)
getElement = root.getElement(sPath)
end function
public function getChildNodes(sPath)
getChildNodes = split(root.getChildNodes(sPath), ",")
end function
public function elementExists(sElement)
elementExists = root.elementExists(sElement)
end function
end class
%>
<script language="javascript" runat="server" src="json2.js"></script>
<script language="javascript" runat="server">
function new_JsonEngine(sJson) {
return new JsonEngine(sJson);
};
function JsonEngine(sJson) {
var me = this;
this.data = {};
this.error = false;
this.initialize = function(sJson) {
try {
this.data = JSON.parse(sJson);
} catch (e) {
me.error = true;
}
};
this.elementExists = function (sElement) {
var currentRoot = me.data;
var aPath = sElement.split('.');
var bExists = true;
for (var i = 0, len = aPath.length; i < len; i++) {
currentRoot = currentRoot[aPath[i]];
if (typeof currentRoot === "undefined") {
bExists = false;
break;
}
}
return bExists;
};
this.getElement = function(sPath) {
var node = me.data;
var aPath = sPath.split('.');
for(var i = 0, len = aPath.length; i < len; i++) {
node = node[aPath[i]];
}
return (typeof node == "object" && node.length)? "[object Array]" : node;
};
this.getChildNodes = function(sPath) {
var keys = [];
var parentNode = me.data;
if( sPath.length > 0 ) {
var aPath = sPath.split('.');
for(var i = 0, len = aPath.length; i < len; i++) {
parentNode = parentNode[aPath[i]];
}
}
for(var key in parentNode) {
(sPath.length > 0)? keys.push(sPath + "." + key) : keys.push(key);
}
return keys;
};
this.hasError = function() {
return me.error;
};
this.initialize(sJson);
}
</script>
Upvotes: 0
Reputation: 1
' >>> A hundred times faster!
public function GetJsonObject( str_json )
dim obj_script: Set obj_script = CreateObject("ScriptControl")
obj_script.Language = "JScript"
obj_script.Eval("var obj_json = " & str_json)
set GetJsonObject = obj_script.Eval("obj_json")
end function
dim str_json: str_json = "{ date :'4/28/2017', custType: 100, vehicle: 1 }"
dim obj_json: set obj_json = GetJsonObject(str_json)
response.write("date :" & obj_json.date & "<br />")
response.write("custType :" & obj_json.custType & "<br />")
response.write("vehicle :" & obj_json.vehicle & "<br />")
' >>> no lib to include and when the JSON is very long, it works super fast also, not with lib "aspJSON". Plus fields can be read directly, no string like xxx("vehicle")
Upvotes: 0
Reputation: 11086
As a quick solution for simple json structure and escaped values, this simple function returns the key values by splitting the json string on double quotes chr(34)
:
function getKeyValue(JsonString,key)
myarray=split(JsonString,key,-1,1)
if ubound(myarray)>0 then
myarray2=split(myarray(1),chr(34),-1,1)
getKeyValue=myarray2(2)
else
getKeyValue=""
end if
end function
usage:
response.write("<li> date :" & getKeyValue(Your_Json_String_Here,"date") & "</li>")
Upvotes: 2
Reputation: 24488
Got it working:
Using https://github.com/rcdmk/aspJSON
Dim jsonString
jsonString = {"date":"4/28/2017","custType":"100","vehicle":"1"}
Dim jsonObj, outputObj
set jsonObj = new JSONobject
set outputObj = jsonObj.parse(jsonString)
response.write("<li> date :" & outputObj("date") & "</li>")
response.write("<li> custType :" & outputObj("custType") & "</li>")
response.write("<li> vehicle :" & outputObj("vehicle") & "</li>")
If you need to iterate through the single object use outputObj.pairs
Dim props, prop
props = outputObj.pairs
for each prop in props
response.write prop.name & " : " & prop.value & "<br>"
next
as referenced https://github.com/rcdmk/aspJSON/issues/20
Upvotes: 5