asp json类库解析json字符串

  asp将json格式的字符串生成asp对象类库,asp如何解析json格式的字符串,asp如何读写json类库

  asp读取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"
    }
  ]
}

  aspjson类库使用示例

<!--#include virtual="/aspJSON1.17.asp" -->
<%
Set oJSON = New aspJSON
'加载json字符串生成对象
oJSON.loadJSON(jsonstring)
'普通类型
Response.Write oJSON.data("firstName") & "<br>"
set address=oJSON.data("address") 'json对象
Response.Write address("streetAddress")& "<br>"
Response.Write address("city") & "<br>"
'json数组,需要遍历
For Each phonenr In oJSON.data("phoneNumber")
    Set this = oJSON.data("phoneNumber").item(phonenr)
    Response.Write _
    this.item("type") & ": " & _
    this.item("number") & "<br>"
Next
'Update/Add value
oJSON.data("firstName") = "James"
'Return json string
Response.Write oJSON.JSONoutput()
%>

 

aspjson写json对象示例

<!--#include virtual="/aspJSON1.17.asp" -->
<%
Set oJSON = New aspJSON
With oJSON.data
    .Add "familyName", "Smith"                      'Create value
    .Add "familyMembers", oJSON.Collection()
    With oJSON.data("familyMembers")
        .Add 0, oJSON.Collection()                  'Create object
        With .item(0)
            .Add "firstName", "John"
            .Add "age", 41
        End With
        .Add 1, oJSON.Collection()
        With .item(1)
            .Add "firstName", "Suzan"
            .Add "age", 38
            .Add "interests", oJSON.Collection()    'Create array
            With .item("interests")
                .Add 0, "Reading"
                .Add 1, "Tennis"
                .Add 2, "Painting"
            End With
        End With
        .Add 2, oJSON.Collection()
        With .item(2)
            .Add "firstName", "John Jr."
            .Add "age", 2.5
        End With
    End With
End With
Response.Write oJSON.JSONoutput()                   'Return json string
%>

  返回的json格式的字符串

{
  "familyName": "Smith",
  "familyMembers":
  [
    {
      "firstName": "John",
      "age": 41
    },
    {
      "firstName": "Suzan",
      "age": 38,
      "interests":
      [
        "Reading",
        "Tennis",
        "Painting"
      ]
    },
    {
      "firstName": "John Jr.",
      "age": 2.5
    }
  ]
}

aspJSON1.17.asp下载

 

 

更加复杂数据读取示例

json.txt

 

{
    "item": [
        {
            "media_id": "111111111111",
            "content": {
                "news_item": [
                    {
                        "title": "aaaaaaaaaaa111111111111",
                        "author": "",
                        "digest": "aaaaa",
                        "content": "dasfasfasdf",
                        "content_source_url": "",
                        "thumb_media_id": "222222222222222211111111111",
                        "show_cover_pic": 0,
                        "url": "dddddddddddddd2222222222222222",
                        "thumb_url": "ffffffffffffffssssssssssssssssss"
                    }
                ],
                "create_time": 1454771038,
                "update_time": 1454771234
            },
            "update_time": 1454771234
        },
        {
            "media_id": "222222222222222222222222",
            "content": {
                "news_item": [
                    {
                        "title": "bbbb",
                        "author": "",
                        "digest": "dddddddddddddd",
                        "content": "dddddddddffffffffffffff",
                        "content_source_url": "",
                        "thumb_media_id": "OKYCWs9AsE4iVttOm0C3ktJUiBgJ766yZoHoiGkxeA0",
                        "show_cover_pic": 0,
                        "url": "hhhhhhhhhhhhhhhhh",
                        "thumb_url": "ggggggggggggggggggggggggg"
                    }
                ],
                "create_time": 1454683014,
                "update_time": 1454683127
            },
            "update_time": 1454683127
        },
        {
            "media_id": "33333333333333",
            "content": {
                "news_item": [
                    {
                        "title": "ccccccccccccc",
                        "author": "",
                        "digest": "ccccccccccccccccccvvvvvvvvvvvvvvvvvv",
                        "content_source_url": "",
                        "thumb_media_id": "vcvcvcv",
                        "show_cover_pic": 0,
                        "url": "vvvvvvvvvvvvvvcccccccccccccc",
                        "thumb_url": "kkkkkkkkkkkkkvvvvvvvvvvvvv"
                    }
                ],
                "create_time": 1447315546,
                "update_time": 1453449705
            },
            "update_time": 1453449705
        }
    ],
    "total_count": 4,
    "item_count": 4
}

read.asp

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="aspJSON1.17.asp"-->
<%
function readcontent(path)
  dim fso,txtstream
  set fso=server.CreateObject("scripting.filesystemobject")
  set txtstream=fso.OpenTextFile(path)
  readcontent=txtstream.readall
  txtstream.close
  set txtstream=nothing
  set fso=nothing
end function
s=readcontent(server.MapPath("json.txt"))
Set oJSON = New aspJSON
oJSON.loadJSON(s)
response.Write "总数:"&oJSON.data("total_count")&"<br>新闻列表<hr>"
for each itemindex in oJSON.data("item")'遍历item数组
  set item=oJSON.data("item").item(itemindex)
  response.Write "media_id:"&item.item("media_id")&"<br>"
  set content=item.item("content")
  for each newsindex in content.item("news_item")
    set newsitem=content.item("news_item").item(newsindex)
    response.Write  "title:"&newsitem.item("title")&"<br>"
    response.Write  "author:"&newsitem.item("author")&"<br>"
    response.Write  "content:"&newsitem.item("content")&"<br>"
    '''更多属性。。。
  next
  response.Write "<hr>"
next
%>

来源:http://www.aspjson.com/

评论(0)网络
阅读(1495)喜欢(0)不喜欢(0)Asp/VBScript