查看: 1666|回复: 4

[文章教程] JSON 2.0.4.asp解析类基本用法范例

[复制链接]
ttasp 发表于 2019-6-28 19:51:54 | 显示全部楼层 |阅读模式
  1. <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
  2. <!--#include file="JSON_2.0.4.asp" -->
  3. <%
  4. Response.Charset = "utf-8"
  5. Response.ContentType = "text/JSON"

  6. Dim Json
  7. Set Json = jsObject()
  8. Json("type") = 1               
  9.         
  10. Set Json("item")= jsArray() '数组对像   
  11.    Set Json("item")(Null) = jsObject()
  12.          Json("item")(Null)("id")=20   '{"id":20,"title":"这是一个标题"},
  13.          Json("item")(Null)("title")="这是一个标题"
  14.                  
  15.   Set Json("item")(Null) = jsObject()
  16.          Json("item")(Null)("id")=21   '{"id":21,"title":"这是一个标题1"},
  17.          Json("item")(Null)("title")="这是一个标题2"

  18. Json.Flush
  19. Set Json = Nothing
  20. %>
复制代码


输出格式:
  1. {
  2.         "type":1,
  3.         "item":[
  4.                 {"id":20,"title":"这是一个标题"},
  5.                 {"id":21,"title":"这是一个标题2"}
  6.                 ]
  7. }
复制代码


验证效果:
QQ截图20190628195204.png

Site_E.rar (2.03 KB, 下载次数: 0, 售价: 10 个ASP币)
大熊 发表于 2019-6-28 20:33:25 | 显示全部楼层
l 这效率 杠杠的
而立而已 发表于 2019-12-11 22:22:28 | 显示全部楼层
本帖最后由 而立而已 于 2019-12-11 22:36 编辑

JSON 2.0.4 还是差点了 我自己重新优化修改了 感觉要比现在的好用多了
而立而已 发表于 2019-12-11 22:24:20 | 显示全部楼层
<!--#INCLUDE file="Class_JSON.asp" -->
<%
Response.Write "JSON数据操作类可对各种复杂JSON,读取、生成、删除 By HT.Liu"&vbCrLf
Response.Write "-----------------------------------------------------"&vbCrLf
jsonstring = _
"{"& _
  """firstName"": ""John"","& _
  """lastName"" : ""Smith"","& _
  """age""      : 25,"& _
  """address""  :"& _
  "{"& _
    """streetAddress"": ""21 2nd Street"","& _
    """city""         : ""New York"","& _
    """state""        : ""NY"","& _
    """postalCode""   : ""10021"""& _
  "},"& _
  """phoneNumber"":"& _
  "["& _
    "{"& _
        """type""  : ""home"","& _
        """number"": ""212 555-1234"""& _
      "},"& _
      "{"& _
        """type""  : ""fax"","& _
       " ""number"": ""646 555-4567"""& _
    "}"& _
  "],"& _
  """rows"":"& _
  "["& _
    "["& _
                "1,"& _
                "2,"& _
                "3"& _
    "],"& _
    "["& _
                "4,"& _
                "5,"& _
                "6"& _
    "]"& _
  "]"& _  
"}"
Set ReadJSON = New LHT_JSON
'载入Json数据
ReadJSON.Load(jsonstring)
Response.Write "JSON原始数据"&vbCrLf
Response.Write "-----------------------------------------------------"&vbCrLf
Response.Write ReadJSON.ToJson()&""&vbCrLf
Response.Write "-----------------------------------------------------"&vbCrLf
'获得Json数据
Response.Write "JSON数据读取"&vbCrLf
Response.Write "-----------------------------------------------------"&vbCrLf
Response.Write "firstName:" & ReadJSON("firstName") & ""&vbCrLf
Response.Write "lastName:" & ReadJSON("lastName") & ""&vbCrLf
Response.Write "age:" & ReadJSON("age") & ""&vbCrLf

Response.Write "address.streetAddress:" & ReadJSON("address").item("streetAddress") & ""&vbCrLf
Response.Write "phoneNumber[0].type:" & ReadJSON("phoneNumber").item(0).item("type") & ""&vbCrLf
Response.Write "phoneNumber[1].type:" & ReadJSON("phoneNumber").item(1).item("type") & ""&vbCrLf

Response.Write "rows[0][0]:" & ReadJSON("rows")(0)(0) & ""&vbCrLf
Response.Write "rows[1][0]:" & ReadJSON("rows")(1)(0) & ""&vbCrLf
Response.Write "rows长度:" & ReadJSON("rows").Count & ""&vbCrLf
'循环读取数组
For Each phonenr In ReadJSON("phoneNumber")
    Set this = ReadJSON("phoneNumber")(phonenr)
    Response.Write _
    this("type") & ": " & _
    this("number") & ""&vbCrLf
Next
For Each rows In ReadJSON("rows")
    Set this = ReadJSON("rows").item(rows)
    Response.Write "rows["&rows&"]:" & _
    this.item(0) & "-" & _
    this.item(1) & "-"& _
        this.item(2) & ""&vbCrLf
Next
Response.Write "-----------------------------------------------------"&vbCrLf
'获得Json数据
Response.Write "在原始数据基础上更新、删除,并生成新的JSON"&vbCrLf
Response.Write "-----------------------------------------------------"&vbCrLf
'更新或添加数值
ReadJSON("firstName") = "HaiTao"
ReadJSON("lastName") = "Liu"
ReadJSON("age") = 38
'删除数值
ReadJSON.data.Remove("rows")

'返回Json字符串
Response.Write "输出格式:多行"&vbCrLf&vbCrLf
Response.Write ReadJSON.ToJson()&""&vbCrLf
Response.Write "输出格式:单行压缩"&vbCrLf&vbCrLf
Response.Write ReadJSON.ToString()&""&vbCrLf'同.Flush
Response.Write "-----------------------------------------------------"&vbCrLf
Response.Write "多方式生成JSON,操作多样化,选一种喜欢的"&vbCrLf
Response.Write "-----------------------------------------------------"&vbCrLf
Response.Write "类底层基础操作,其它方式均在此基础上优化"&vbCrLf
Response.Write "-----------------------------------------------------"&vbCrLf
Set WriteJSON = New LHT_JSON
With WriteJSON.data

    .Add "familyName", "美好一家"                              '创建普通数值
    .Add "msg", "OK"
    .Add "total", 3
    .Add "familyMembers", WriteJSON.Object()

    With WriteJSON("familyMembers")

        .Add 0, WriteJSON.Object()                  '创建对象
        With .item(0)
            .Add "firstName", "John"
            .Add "age", 41
        End With

        .Add 1, WriteJSON.Object()
        With .item(1)
            .Add "firstName", "Suzan"
            .Add "age", 38
            .Add "interests", WriteJSON.Object()    '创建数组
            With .item("interests")
                .Add 0, "Reading"
                .Add 1, "Tennis"
                .Add 2, "Painting"
            End With
        End With

        .Add 2, WriteJSON.Object()
        With .item(2)
            .Add "firstName", "John Jr."
            .Add "age", 2.5
        End With

    End With
End With

Response.Write WriteJSON.ToJson()&""&vbCrLf
Response.Write "-----------------------------------------------------"&vbCrLf
Response.Write "Asp基本赋值方式(1)-生成数据如下:"&vbCrLf
Response.Write "-----------------------------------------------------"&vbCrLf
Set AspObj = New LHT_JSON
'批量赋值
With AspObj
        .data("familyName")        = "美好一家"
        .data("msg")                = "OK"
End With
'独立赋值
AspObj.data("total")        = 3
'创建一个对象
Set AspObj.data("familyMembers") = AspObj.Object()
Set AspObj.data("familyMembers").item(0) = AspObj.Object()
With AspObj("familyMembers")
                .item(0)("firstName")        = "John"
                .item(0)("age")                        = 41
End With

Set AspObj.data("familyMembers").item(1) = AspObj.Object()
With AspObj("familyMembers")
                .item(1)("firstName")        = "Suzan"
                .item(1)("age")                        = 38
                Set .item(1)("interests")        = AspObj.Object()
End With

With AspObj("familyMembers").item(1)("interests")
        .item(0) = "Reading"
        .item(1) = "Tennis"
        .item(2) = "Painting"               
End With
       
Set AspObj.data("familyMembers").item(2) = AspObj.Object()
With AspObj("familyMembers")
                .item(2)("firstName")        = "John Jr."
                .item(2)("age")                        = 2.5
End With
Response.Write AspObj.ToJson()&""&vbCrLf
Response.Write "-----------------------------------------------------"&vbCrLf
Response.Write "Asp快速赋值方式(2)-生成数据如下:"&vbCrLf
Response.Write "-----------------------------------------------------"&vbCrLf
Set QObj = New LHT_JSON

QObj("familyName")        = "美好一家"
QObj("msg")                        = "OK"
QObj("total")                = 3

'创建一个对象
Set QObj("familyMembers") = QObj.Object()
With QObj("familyMembers")
                Set .item(0) = QObj.Object()
                .item(0)("firstName")        = "John"
                .item(0)("age")                        = 41
               
                Set .item(1) = QObj.Object()
                .item(1)("firstName")        = "Suzan"
                .item(1)("age")                        = 38
               
                Set .item(1)("interests")        = QObj.Object()
                With .item(1)("interests")
                        .item(0) = "Reading"
                        .item(1) = "Tennis"
                        .item(2) = "Painting"               
                End With
               
                Set .item(2) = QObj.Object()
                .item(2)("firstName")        = "John Jr."
                .item(2)("age")                        = 2.5
End With

Response.Write QObj.ToJson()&""&vbCrLf
Response.Write "-----------------------------------------------------"&vbCrLf
Response.Write "Asp直观赋值方式(3)-生成数据如下:"&vbCrLf
Response.Write "-----------------------------------------------------"&vbCrLf
Set JObj = New LHT_JSON

JObj("familyName")        = "美好一家"
JObj("msg")                        = "OK"
JObj("total")                = 3

'创建一个对象
Set JObj("familyMembers") = JObj.Object()

Set JObj("familyMembers")(0) = JObj.Object()
JObj("familyMembers")(0)("firstName")        = "John"
JObj("familyMembers")(0)("age")                        = 41

Set JObj("familyMembers")(1) = JObj.Object()
JObj("familyMembers")(1)("firstName")        = "Suzan"
JObj("familyMembers")(1)("age")                        = 38
Set JObj("familyMembers")(1)("interests") = JObj.Object()        '数组
JObj("familyMembers")(1)("interests")(0) = "Reading"
JObj("familyMembers")(1)("interests")(1) = "Tennis"
JObj("familyMembers")(1)("interests")(2) = "Painting"               

Set JObj("familyMembers")(2) = JObj.Object()
JObj("familyMembers")(2)("firstName")        = "John Jr."
JObj("familyMembers")(2)("age")                        = 2.5
Response.Write JObj.ToJson()&""

%>
而立而已 发表于 2019-12-11 22:24:37 | 显示全部楼层
生成的数据

JSON数据操作类可对各种复杂JSON,读取、生成、删除 By HT.Liu
-----------------------------------------------------
JSON原始数据
-----------------------------------------------------
{
    "firstName": "John",
    "lastName": "Smith",
    "age": 25,
    "address": {
        "streetAddress": "21 2nd Street",
        "city": "New York",
        "state": "NY",
        "postalCode": "10021"
    },
    "phoneNumber": [
        {
            "type": "home",
            "number": "212 555-1234"
        },
        {
            "type": "fax",
            "number": "646 555-4567"
        }
    ],
    "rows": [
        [
            1,
            2,
            3
        ],
        [
            4,
            5,
            6
        ]
    ]
}
-----------------------------------------------------
JSON数据读取
-----------------------------------------------------
firstName:John
lastName:Smith
age:25
address.streetAddress:21 2nd Street
phoneNumber[0].type:home
phoneNumber[1].type:fax
rows[0][0]:1
rows[1][0]:4
rows长度:2
home: 212 555-1234
fax: 646 555-4567
rows[0]:1-2-3
rows[1]:4-5-6
-----------------------------------------------------
在原始数据基础上更新、删除,并生成新的JSON
-----------------------------------------------------
输出格式:多行

{
    "firstName": "HaiTao",
    "lastName": "Liu",
    "age": 38,
    "address": {
        "streetAddress": "21 2nd Street",
        "city": "New York",
        "state": "NY",
        "postalCode": "10021"
    },
    "phoneNumber": [
        {
            "type": "home",
            "number": "212 555-1234"
        },
        {
            "type": "fax",
            "number": "646 555-4567"
        }
    ]
}
输出格式:单行压缩

{"firstName":"HaiTao","lastName":"Liu","age":38,"address":{"streetAddress":"21 2nd Street","city":"New York","state":"NY","postalCode":"10021"},"phoneNumber":[{"type":"home","number":"212 555-1234"},{"type":"fax","number":"646 555-4567"}]}
-----------------------------------------------------
多方式生成JSON,操作多样化,选一种喜欢的
-----------------------------------------------------
类底层基础操作,其它方式均在此基础上优化
-----------------------------------------------------
{
    "familyName": "\u7F8E\u597D\u4E00\u5BB6",
    "msg": "OK",
    "total": 3,
    "familyMembers": [
        {
            "firstName": "John",
            "age": 41
        },
        {
            "firstName": "Suzan",
            "age": 38,
            "interests": [
                "Reading",
                "Tennis",
                "Painting"
            ]
        },
        {
            "firstName": "John Jr.",
            "age": 2.5
        }
    ]
}
-----------------------------------------------------
Asp基本赋值方式(1)-生成数据如下:
-----------------------------------------------------
{
    "familyName": "\u7F8E\u597D\u4E00\u5BB6",
    "msg": "OK",
    "total": 3,
    "familyMembers": [
        {
            "firstName": "John",
            "age": 41
        },
        {
            "firstName": "Suzan",
            "age": 38,
            "interests": [
                "Reading",
                "Tennis",
                "Painting"
            ]
        },
        {
            "firstName": "John Jr.",
            "age": 2.5
        }
    ]
}
-----------------------------------------------------
Asp快速赋值方式(2)-生成数据如下:
-----------------------------------------------------
{
    "familyName": "\u7F8E\u597D\u4E00\u5BB6",
    "msg": "OK",
    "total": 3,
    "familyMembers": [
        {
            "firstName": "John",
            "age": 41
        },
        {
            "firstName": "Suzan",
            "age": 38,
            "interests": [
                "Reading",
                "Tennis",
                "Painting"
            ]
        },
        {
            "firstName": "John Jr.",
            "age": 2.5
        }
    ]
}
-----------------------------------------------------
Asp直观赋值方式(3)-生成数据如下:
-----------------------------------------------------
{
    "familyName": "\u7F8E\u597D\u4E00\u5BB6",
    "msg": "OK",
    "total": 3,
    "familyMembers": [
        {
            "firstName": "John",
            "age": 41
        },
        {
            "firstName": "Suzan",
            "age": 38,
            "interests": [
                "Reading",
                "Tennis",
                "Painting"
            ]
        },
        {
            "firstName": "John Jr.",
            "age": 2.5
        }
    ]
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表

在线客服

售前咨询
售后咨询
服务热线
023-58418553
微信公众号