ASP 계층형 JSON 출력하기
사용한 라이브러리
- JSON_2.0.4.asp
- DBHelper.asp
json 다운로드 주소 : https://code.google.com/archive/p/aspjson/downloads
dbhelper 다운로드 주소 : http://taeyo.net/Lecture/20_Tips/DBHelper.asp
json array 참고 사이트 : https://www.mikesdotnetting.com/article/98/ajax-with-classic-asp-using-jquery
------------------------------------------------------------------------------------------------------------------------------------------
Dim oJSON_Main
set oJSON_Main = jsObject()
totalcount = modelDAO.TotalCount()
If (totalcount > 0) Then
SET datalist = modelDAO.List()
End If
SET oJSON_sub = jsObject()
oJSON_sub("totalcount") = totalcount
SET oJSON_sub("datalist") = datalist
SET oJSON_Main = oJSON_sub
Response.Clear
Response.ContentType = "text/plain;charset=utf-8"
Response.CharSet = "UTF-8"
Response.Write toJSON(oJSON_Main)
Response.End
-----------------------------------------------------------------------------------
'클래스 생성 후 클래스에서 불러옴
'부모 리스트
PUBLIC FUNCTION List()
strSQL = " "
strSQL = strSQL & " 실행 쿼리문 "
SET objRs = DBHelper.ExecSQLReturnRS(strSQL, nothing, nothing)
Dim jsa, col
Set jsa = jsArray()
if objRs.BOF or objRs.EOF then '데이터가 없을 경우
set List = jsa
ELSE '데이터가 있을 경우
do until objRs.eof
Set jsa(Null) = jsObject()
For Each col In objRs.Fields
jsa(Null)(col.Name) = col.Value
set jsa(Null)("subdatalist") = Sub_List(objRs(0))
Next
objRs.movenext
loop
'return
set List = jsa
end if
objRs.close
set objRs = nothing
Set jsa = nothing
END FUNCTION
'자식 리스트
PUBLIC FUNCTION Sub_List( seq )
strSQL = " "
strSQL = strSQL & " 자식 쿼리 "
Dim params(0)
params(0) = DBHelper.MakeParam("@parameter", adInteger, adParamInput, , seq)
Dim objRs
SET objRs = DBHelper.ExecSQLReturnRS(strSQL, params, nothing)
Dim jsa,col
Set jsa = jsArray()
if objRs.BOF or objRs.EOF then '데이터가 없을 경우
set Sub_List = jsa
ELSE '데이터가 있을 경우
do until objRs.eof
Set jsa(Null) = jsObject()
For Each col In objRs.Fields
jsa(Null)(col.Name) = col.Value
Next
objRs.movenext
loop
'return
set Sub_List = jsa
end if
objRs.close
set objRs = nothing
Set jsa = nothing
END FUNCTION
<%
response.write "{"
response.write """docType"":""new"""
response.write ",""rowNum"":""10"""
for i =0 to 10 step 1
response.write ",""tit"&i&""":""number "&i&""""
response.write ",""tit"&i&""":""number "&i&""""
next
response.write "}"
%>
<%
response.write "{"
response.write """docType"":""new"""
response.write ",""rowNum"":""10"""
response.write "}"
%>
<%
response.write "{"
While Not rs.EOF
IDX = Rs("IDX")
PHARMNAME = Rs("PHARMNAME")
INDATE = Rs("INDATE")
TDAYS = Rs("TDAYS")
DRUGCODE = Rs("DRUGCODE")
DRUGNAME = Rs("DRUGNAME")
SPLNAME = Rs("SPLNAME")
PGUBUN = Rs("PGUBUN")
QUAN = Rs("QUAN")
QUAN_TIME = Rs("QUAN_TIME")
DAYS = Rs("DAYS")
KPIC_CODE = Rs("KPIC_CODE")
response.write "Index:""" & IDX & """"
response.write ",""PharmName:""" & PHARMNAME & """"
response.write ",""InDate:""" & INDATE & """"
response.write ",""TDays:""" & TDAYS & """"
response.write ",""DrugCode:""" & DRUGCODE & """"
response.write ",""DrugName:""" & DRUGNAME & """"
response.write ",""SPLName:""" & SPLNAME & """"
response.write ",""PGubun:""" & PGUBUN & """"
response.write ",""Quan:""" & QUAN & """"
response.write ",""Quan_Time:""" & QUAN_TIME & """"
response.write ",""Days:""" & DAYS & """"
response.write ",""KPIC_Code:""" & KPIC_CODE & """"
Rs.MoveNext
Wend
response.write "}"
%>
<%
Rs.Close
dbConn.close
%>
response.write ",""Index:""" & IDX & """"
response.write ",""PharmName:""" & PHARMNAME & """"
response.write ",""InDate:""" & INDATE & """"
response.write ",""TDays:""" & TDAYS & """"
response.write ",""DrugCode:""" & DRUGCODE & """"
response.write ",""DrugName:""" & DRUGNAME & """"
response.write ",""SPLName:""" & SPLNAME & """"
response.write ",""PGubun:""" & PGUBUN & """"
response.write ",""Quan:""" & QUAN & """"
response.write ",""Quan_Time:""" & QUAN_TIME & """"
response.write ",""Days:""" & DAYS & """"
response.write ",""KPIC_Code:""" & KPIC_CODE & """"
<%= IDX & "^" & PHARMNAME & "^" & INDATE & "^" & TDAYS & "^" & DRUGCODE & "^" & DRUGNAME & "^" & SPLNAME & "^" & PGUBUN & "^" & QUAN & "^" & QUAN_TIME & "^" & DAYS & "^" & KPIC_CODE %>
<%
response.write "{"
response.write """Index"":""IDX"""
response.write ",""PharmName"":""PHARMNAME"""
response.write ",""InDate"":""INDATE"""
response.write ",""TDays"":""TDAYS"""
response.write ",""DrugCode"":""DRUGCODE"""
response.write ",""DrugName"":""DRUGNAME"""
response.write ",""SPLName"":""SPLNAME"""
response.write ",""Pgubun"":""PGUBUN"""
response.write ",""Quan"":""QUAN"""
response.write ",""Quan_Time"":""QUAN_TIME"""
response.write ",""Days"":""DAYS"""
response.write ",""KPIC_Code"":""KPIC_CODE"""
response.write "}"
%>
RecordCount
[{"a="1","aa=10"},{b=1,20},{c,30}]
<%@ codepage="949" language="VBScript" %><%
'session.codepage = 65001 ' codepage="949" language
Session.CodePage = 949
'Response.expires=-1
Response.ContentType = "application/json"
Response.Charset = "euc-kr"
'Response.Charset = "utf-8"
Dim ConnectionString
ConnectionString="Provider=SQLOLEDB;Data Source=~~~~;"
Set DB=Server.CreateObject("ADODB.Connection")
DB.Open ConnectionString
'=== 메인 정보 입력 ===============
ViewTbl = "TB_VIEW_~~~~~"
OrderBy = "ORDER BY str_manufacture " '제조사 정렬
'==================================
SQL = "SELECT * FROM " & ViewTbl & " " & OrderBy
Set Rs = DB.Execute(SQL)
%>{device:[<%
CurCom = ""
Do While Not Rs.EOF
If CurCom <> Rs("str_manufacture") Then
If CurCom <> "" Then
response.write "]},"
End IF
response.write "{company:'"& Rs("str_manufacture") &"',site:'"& Rs("str_ma_homepage") &"',product:["
CurCom = Rs("str_manufacture")
Else
response.write ","
End if
response.write "{maker:""" & Rs("str_manufacture") & """"
response.write ",brand:""" & Rs("str_product") & """"
response.write ",model:'" & Rs("str_model") & "'"
response.write ",firmware:""" & Rs("str_fw_version") & """"
response.write ",commt:""" & Rs("str_comment") & """"
response.write ",image:""" & Rs("str_image") & """}"
Rs.MoveNext
Loop
DB.Close
Set DB =Nothing
Set Rs = Nothing
%>]}]}
한글처리 관련해서....
<%@ codepage="949" language="VBScript" %><%
Session.CodePage = 949
'Response.expires=-1
Response.ContentType = "application/json"
Response.Charset = "euc-kr"
만일 UTF-8 이면
<%@ codepage="65001" language="VBScript" %><%
session.codepage = 65001
'Response.expires=-1
Response.ContentType = "application/json"
Response.Charset = "utf-8"
'Web' 카테고리의 다른 글
6.1 서블릿의 요청 기능 실습 (1) | 2022.12.07 |
---|---|
5.7~8 애너테이션을 이용한 서블릿 매핑 (0) | 2022.12.06 |
5.5~6 다수의 서블릿 매핑하기 (0) | 2022.12.05 |
5.3~4 서블릿 실습 과정 (0) | 2022.12.01 |
5.2 서블릿의 계층구조와 기능 (0) | 2022.11.30 |
댓글