Skip to content

Instantly share code, notes, and snippets.

@mapserver2007
Last active December 22, 2015 17:29
Show Gist options
  • Save mapserver2007/6506956 to your computer and use it in GitHub Desktop.
Save mapserver2007/6506956 to your computer and use it in GitHub Desktop.
個人用。
<%
Public Function stringify(ByVal VariantValue)
Dim JsonStr
Dim DictionaryValue, ArrayValue
Dim Key, Value
Dim TypeNumber
Dim i
JsonStr = ""
TypeNumber = VarType(VariantValue)
If TypeNumber = vbObject Then
Set DictionaryValue = VariantValue
For Each Key In DictionaryValue.Keys
JsonStr = JsonStr & """" & Key & """"
JsonStr = JsonStr & ":"
'プリミティブ型かオブジェクト型かで分ける
TypeNumber = VarType(DictionaryValue.Item(Key))
Select Case TypeNumber
Case vbString, vbInteger, vbLong, vbDouble, vbSingle, vbCurrency, vbDate, vbBoolean, vbByte
Value = DictionaryValue.Item(Key)
JsonStr = JsonStr & """" & CStr(Value) & ""","
Case vbObject
Set Value = DictionaryValue.Item(Key)
JsonStr = JsonStr & stringify(Value) & ","
Case vbNull
JsonStr = JsonStr & "null,"
Case vbEmpty
JsonStr = JsonStr & """"","
Case Else
If TypeNumber >= vbArray Then
Value = DictionaryValue.Item(Key)
JsonStr = JsonStr & stringify(Value) & ","
End If
End Select
Next
JsonStr = Mid(JsonStr, 1, Len(JsonStr) - 1)
stringify = "{" & JsonStr & "}"
ElseIf TypeNumber >= vbArray Then
ArrayValue = VariantValue
JsonStr = JsonStr & "["
For i = 0 To UBound(ArrayValue)
Value = VariantValue(i)
TypeNumber = VarType(Value)
Select Case TypeNumber
Case vbString, vbInteger, vbLong, vbDouble, vbSingle, vbCurrency, vbDate, vbBoolean, vbByte
JsonStr = JsonStr & """" & CStr(Value) & ""","
Case vbObject
JsonStr = JsonStr & stringify(Value) & ","
Case vbNull
JsonStr = JsonStr & "null,"
Case Else
If TypeNumber >= vbArray Then
JsonStr = JsonStr & stringify(Value) & ","
End If
End Select
Next
JsonStr = Mid(JsonStr, 1, Len(JsonStr) - 1)
JsonStr = JsonStr & "]"
stringify = JsonStr
End If
End Function
%>
<% @ Language="VBScript" %>
<%
Option Explicit
Response.Buffer = True
%>
<!-- #INCLUDE FILE = "stringify.asp" -->
<%
Dim Json
Dim Dic, Dic2, Dic3, Result, ArrayValue, ArrayValue2
Set Result = Server.CreateObject("Scripting.Dictionary")
' --------------------------------------------------------------------
' 1.文字列型の値を入れ子なしで設定したDictionaryをJSONに変換できること
' --------------------------------------------------------------------
Set Dic = Server.CreateObject("Scripting.Dictionary")
Dic.Add "key1", "value1"
Dic.Add "key2", "value2"
Result.Add "Test1", stringify(Dic)
' --------------------------------------------------------------------
' --------------------------------------------------------------------
' 2.数値(整数)型の値を入れ子なしで設定したDictionaryをJSONに変換できること
' --------------------------------------------------------------------
Set Dic = Server.CreateObject("Scripting.Dictionary")
Dic.Add "key1", 10
Dic.Add "key2", -9
Dic.Add "key3", 1000000000000000000000000000000000000001
Result.Add "Test2", stringify(Dic)
' --------------------------------------------------------------------
' --------------------------------------------------------------------
' 3.数値(浮動小数点)型の値を入れ子なしで設定したDictionaryをJSONに変換できること
' --------------------------------------------------------------------
Set Dic = Server.CreateObject("Scripting.Dictionary")
Dic.Add "key1", 1.1
Dic.Add "key2", 0.00000123
Dic.Add "key3", -123.23
Result.Add "Test3", stringify(Dic)
' --------------------------------------------------------------------
' --------------------------------------------------------------------
' 4.Empty、Nullを入れ子なしで設定したDictionaryをJSONに変換できること
' --------------------------------------------------------------------
Dim EmptyValue
Set Dic = Server.CreateObject("Scripting.Dictionary")
Dic.Add "key1", EmptyValue
Dic.Add "key2", Null
Result.Add "Test4", stringify(Dic)
' --------------------------------------------------------------------
' --------------------------------------------------------------------
' 5.日付型の値を入れ子なしで設定したDictionaryをJSONに変換できること
' --------------------------------------------------------------------
Set Dic = Server.CreateObject("Scripting.Dictionary")
Dic.Add "key1", Now()
Result.Add "Test5", stringify(Dic)
' --------------------------------------------------------------------
' --------------------------------------------------------------------
' 6.Boolean型の値を入れ子なしで設定したDictionaryをJSONに変換できること
' --------------------------------------------------------------------
Set Dic = Server.CreateObject("Scripting.Dictionary")
Dic.Add "key1", True
Dic.Add "key2", False
Result.Add "Test6", stringify(Dic)
' --------------------------------------------------------------------
' --------------------------------------------------------------------
' 7.配列型の値を入れ子なしかつ型がVariantの場合のDictionaryをJSONに変換できること
' --------------------------------------------------------------------
ReDim ArrayValue(4)
ArrayValue(0) = "string"
ArrayValue(1) = 1000
ArrayValue(2) = 1.1
ArrayValue(3) = True
ArrayValue(4) = Null
Set Dic = Server.CreateObject("Scripting.Dictionary")
Dic.Add "key1", ArrayValue
Result.Add "Test7", stringify(Dic)
' --------------------------------------------------------------------
' --------------------------------------------------------------------
' 8.2重の入れ子かつ各型がプリミティブの場合のDictionaryをJSONに変換できること
' --------------------------------------------------------------------
Set Dic2 = Server.CreateObject("Scripting.Dictionary")
Dic2.Add "key21", "string"
Dic2.Add "key22", 100
Set Dic = Server.CreateObject("Scripting.Dictionary")
Dic.Add "key1", 12345
Dic.Add "key2", Dic2
Result.Add "Test8", stringify(Dic)
' --------------------------------------------------------------------
' --------------------------------------------------------------------
' 9.2重の入れ子かつ各型がArrayの場合のDictionaryをJSONに変換できること
' --------------------------------------------------------------------
ReDim ArrayValue(4)
ArrayValue(0) = "string"
ArrayValue(1) = 1000
ArrayValue(2) = 1.1
ArrayValue(3) = True
ArrayValue(4) = Null
Set Dic2 = Server.CreateObject("Scripting.Dictionary")
Dic2.Add "key21", ArrayValue
Set Dic = Server.CreateObject("Scripting.Dictionary")
Dic.Add "key1", 12345
Dic.Add "key2", Dic2
Result.Add "Test9", stringify(Dic)
' --------------------------------------------------------------------
' --------------------------------------------------------------------
' 10.3重の入れ子かつ各型がVariantの場合のDictionaryをJSONに変換できること
' --------------------------------------------------------------------
ReDim ArrayValue(4)
ArrayValue(0) = "string"
ArrayValue(1) = 1000
ArrayValue(2) = 1.1
ArrayValue(3) = True
ArrayValue(4) = Null
ReDim ArrayValue2(4)
ArrayValue2(0) = "string2"
ArrayValue2(1) = 1001
ArrayValue2(2) = 1.2
ArrayValue2(3) = False
ArrayValue2(4) = Null
Set Dic3 = Server.CreateObject("Scripting.Dictionary")
Dic3.Add "key31", ArrayValue2
Set Dic2 = Server.CreateObject("Scripting.Dictionary")
Dic2.Add "key21", ArrayValue
Dic2.Add "key22", Dic3
Set Dic = Server.CreateObject("Scripting.Dictionary")
Dic.Add "key1", 12345
Dic.Add "key2", Dic2
Result.Add "Test10", stringify(Dic)
' --------------------------------------------------------------------
%>
<html>
<head>
<title>asp test</title>
</head>
<body>
<table id="testTable" border="1">
<tr>
<th>stringify by Clsasp</th>
<th>Test</th>
</tr>
<%Dim i%>
<%For i = 1 To 10%>
<tr>
<td><%=Result.Item("Test" & i)%></td>
<td></td>
</tr>
<%Next%>
</table>
<input type="button" id="execute" value="test!!!!">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$("#execute").on("click", function() {
var isTestOk = false;
$("#testTable tr").children("td").each(function(i) {
if (i % 2 === 0) {
var jsonStr = $(this).text();
try {
if (typeof JSON.parse(jsonStr) === 'object') {
isTestOk = true;
}
}
catch (e) {
isTestOk = false;
}
}
else {
if (isTestOk) {
$(this).css("background-color", "MediumSpringGreen");
}
else {
$(this).css("background-color", "red");
}
isTestOk = false;
}
});
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment