【问题标题】:How can I output a set of files and directories to JSON using Classic ASP?如何使用 Classic ASP 将一组文件和目录输出到 JSON?
【发布时间】:2016-04-26 01:38:03
【问题描述】:

有没有办法可以将提供的树中文件的文件格式输出为 JSON,以便以下 JSON 显示如下。我已经尝试使用http://www.aspjson.com/ 作为参考进行设置,但没有任何运气。有人可以告诉我如何解决这个问题以获得提供的 JSON 结构吗?

=== 文件结构:===

-文档
--fsum
---2007
----教师
-----file1.txt
---2008
----教师2
-----file1.txt
-----file2.txt
----教师3
-----file1.txt
-----file2.txt

=== JSON 示例:===

[
 {year: '2007', report: 'faculty', files: ['file1.txt', 'file2.txt'] },
 {year: '2007', report:'faculty2', files: ['file1.txt', 'file2.txt']},
 {year: 2008, report:'faculty3', files: ['file1.text', 'file2.txt'] }
]

=== 经典 ASP 代码 ===

<%  
dim page, fs, fo, x, c, b, a
page = Server.MapPath("\")
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fo=fs.GetFolder(page & "\docs\fsum\")


' Gets all of the files inside of the docs folder'

for each x in fo.SubFolders
a = a + 1
newFolder = fo & "\" & x.Name
set fob = fs.Getfolder(newFolder)
for each y in fob.SubFolders
b = b + 1
    newFiles = fo & "\" & x.Name & "\" & y.Name
    set foc = fs.GetFolder(newFiles)
    for each z in foc.files 'get all of the files inside  
        c = c + 1
    next
next
next
For counter = 1 to c
  for each x in fo.SubFolders

next
next

set fo=nothing
set fs=nothing

%>

【问题讨论】:

    标签: json asp-classic


    【解决方案1】:

    我发现让它工作的最佳方法是首先了解您希望写入 JSON 的结构。接下来,您应该通过测试对象是否工作来输出来自 http://www.aspjson.com/ 的数据,然后将您自己的值添加到提供的代码中,以便更好地了解您需要在哪里添加 for each 循环。

    以下是其工作原理的示例:

    <!--#include virtual="/aspJSON1.17.asp" -->
    <%
    response.ContentType = "application/json"
    Set oJSON = New aspJSON
    With oJSON.data
    
    Dim fs, fo, page, x
    d = 0 
    page = Server.MapPath("\")
    set fs = Server.CreateObject("Scripting.FileSystemObject")
    set fo = fs.GetFolder(page & "\docs\fsum\")
     'Create value
    .Add 0, oJSON.Collection()
    With oJSON.data(0)
        for each a in fo.subFolders
            x = x + 1
             .Add x, oJSON.Collection() 
            With .item(x)
                .Add "year", a.Name
               newFolder = fo & "\" & a.Name
               set fob = fs.GetFolder(newFolder)
               for each b in fob.SubFolders
               .Add b.Name, oJSON.Collection()
               With .item(b.Name)
                .Add 1, oJSON.Collection() 
                    With .item(1)
                        .Add "files", oJSON.Collection() 
                        With .item("files")
                        newFiles = fo & "\" & a.Name & "\" & b.Name
                        set foc = fs.GetFolder(newFiles)
                        for each z in foc.files 
                        d = d + 1
                            .Add d, z.Name
                        next
                        End With
                    End With
                 End With
                 next
            End With
          next
    End With
    End With
    
    Response.Write oJSON.JSONoutput() 
    %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-23
      • 2011-09-28
      相关资源
      最近更新 更多