在经典ASP中解析json

时间:2015-12-04 22:28:42

标签: json asp-classic

我试图将我们的ERP系统整合到我们的网站中。我能够从系统中获得一些json响应。但我真的需要一些解析它的帮助。

我正在使用ASPJSON(www.aspjson.com)来帮助我。

问题是如何从"数据"中获取所有字段。我能够获得DebtorId,但我无法弄清楚如何获得另一个。

JSON响应:

"Data":{"DebtorId":1,"Data":
[{"Id":"Felt01","Label":"Navn","Value":"Testmedlem"},
{"Id":"Felt14","Label":"Bemærkninger","Value":""},
{"Id":"Felt15","Label":"Oprettet den","Value":null},
{"Id":"Felt16","Label":"Sidst rettet","Value":"2015-12-04T22:56:36"},
{"Id":"Felt23","Label":"Udmeldt","Value":null},
{"Id":"Felt12","Label":"Note","Value":""},
{"Id":"Felt02","Label":"Adresse 1","Value":"Adresse 1"},
{"Id":"Felt03","Label":"Postnr","Value":""},
{"Id":"Felt04","Label":"Bynavn","Value":""},
{"Id":"Felt05","Label":"Mobiltelefon","Value":""},
{"Id":"Felt06","Label":"Adresse 2","Value":""},
{"Id":"Felt07","Label":"Telefon","Value":""},
{"Id":"Felt08","Label":"Efternavn","Value":""},
{"Id":"Felt10","Label":"Email","Value":""},
{"Id":"Felt24","Label":"Aktiv","Value":false},
{"Id":"Felt09","Label":"Grupper og roller","Value":[]},
{"Id":"Felt11","Label":"Medlemskontingent","Value":0},
{"Id":"Felt13","Label":"Medarbejdernr","Value":""},
{"Id":"Felt17","Label":"Ansættelsessted","Value":{"Id":0,"Code":null,"Value":null}},
{"Id":"Felt18","Label":"Land","Value":""},
{"Id":"Felt19","Label":"Medlemsnummer","Value":1},
{"Id":"Felt20","Label":"Donation","Value":0.0},
{"Id":"Felt21","Label":"WebID","Value":0}]},
"WinkasErrorCode":0,"WinKasStatus":0,"WinKasStatusString":"Okay",
"WinKasMessage":null,"ApiVersion":"1.1.888.91",
"ResponseDateTime":"2015-12-04T23:23:19"

经典ASP:

Response.Write "<p>Result (single):" & requestBody.data("DebtorId").item(phonenr) & "</p>"
  For Each phonenr In requestBody.Data
    Set this = requestBody.data("Data").item(Id)
    Response.Write _
    this.item("type") & ": " & _
    this.item("number") & "<br>"
  Next

1 个答案:

答案 0 :(得分:0)

以下代码应该做你想要的。 请注意,我将JSON括在花括号中以创建一个实际的JSON对象。此外,您还必须添加一些代码来区分值节点可以采用的不同形式(“”,[],{})

dim oJSON
Set oJSON = New aspJSON
dim jsonText
call InitJsonText

dim key
dim this

'Load JSON string
oJSON.loadJSON(jsonText)

Response.Write( oJSON.data("Data").item("DebtorId"))

dim dataNode : set dataNode = oJSON.data("Data").item("Data")
' List data array entries
For Each key In dataNode
   Set this  = dataNode.item(key)
   response.Write("key: " & key & ":" & this.item("Id") &  " - "  & this.item("Label") & "<br/>")
Next


sub InitJsonText
  jsonText = _
    "{ " & vbNewLine  & _
    "   ""Data"":{ " & vbNewLine  & _
    "      ""DebtorId"":1, " & vbNewLine  & _
    "      ""Data"":[ " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt01"", " & vbNewLine  & _
    "            ""Label"":""Navn"", " & vbNewLine  & _
    "            ""Value"":""Testmedlem"" " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt14"", " & vbNewLine  & _
    "            ""Label"":""Bemærkninger"", " & vbNewLine  & _
    "            ""Value"":"""" " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt15"", " & vbNewLine  & _
    "            ""Label"":""Oprettet den"", " & vbNewLine  & _
    "            ""Value"":null " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt16"", " & vbNewLine  & _
    "            ""Label"":""Sidst rettet"", " & vbNewLine  & _
    "            ""Value"":""2015-12-04T22:56:36"" " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt23"", " & vbNewLine  & _
    "            ""Label"":""Udmeldt"", " & vbNewLine  & _
    "            ""Value"":null " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt12"", " & vbNewLine  & _
    "            ""Label"":""Note"", " & vbNewLine  & _
    "            ""Value"":"""" " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt02"", " & vbNewLine  & _
    "            ""Label"":""Adresse 1"", " & vbNewLine  & _
    "            ""Value"":""Adresse 1"" " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt03"", " & vbNewLine  & _
    "            ""Label"":""Postnr"", " & vbNewLine  & _
    "            ""Value"":"""" " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt04"", " & vbNewLine  & _
    "            ""Label"":""Bynavn"", " & vbNewLine  & _
    "            ""Value"":"""" " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt05"", " & vbNewLine  & _
    "            ""Label"":""Mobiltelefon"", " & vbNewLine  & _
    "            ""Value"":"""" " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt06"", " & vbNewLine  & _
    "            ""Label"":""Adresse 2"", " & vbNewLine  & _
    "            ""Value"":"""" " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt07"", " & vbNewLine  & _
    "            ""Label"":""Telefon"", " & vbNewLine  & _
    "            ""Value"":"""" " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt08"", " & vbNewLine  & _
    "            ""Label"":""Efternavn"", " & vbNewLine  & _
    "            ""Value"":"""" " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt10"", " & vbNewLine  & _
    "            ""Label"":""Email"", " & vbNewLine  & _
    "            ""Value"":"""" " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt24"", " & vbNewLine  & _
    "            ""Label"":""Aktiv"", " & vbNewLine  & _
    "            ""Value"":false " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt09"", " & vbNewLine  & _
    "            ""Label"":""Grupper og roller"", " & vbNewLine  & _
    "            ""Value"":[ " & vbNewLine  & _
    "            ] " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt11"", " & vbNewLine  & _
    "            ""Label"":""Medlemskontingent"", " & vbNewLine  & _
    "            ""Value"":0 " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt13"", " & vbNewLine  & _
    "            ""Label"":""Medarbejdernr"", " & vbNewLine  & _
    "            ""Value"":"""" " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt17"", " & vbNewLine  & _
    "            ""Label"":""Ansættelsessted"", " & vbNewLine  & _
    "            ""Value"":{ " & vbNewLine  & _
    "               ""Id"":0, " & vbNewLine  & _
    "               ""Code"":null, " & vbNewLine  & _
    "               ""Value"":null " & vbNewLine  & _
    "            } " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt18"", " & vbNewLine  & _
    "            ""Label"":""Land"", " & vbNewLine  & _
    "            ""Value"":"""" " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt19"", " & vbNewLine  & _
    "            ""Label"":""Medlemsnummer"", " & vbNewLine  & _
    "            ""Value"":1 " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt20"", " & vbNewLine  & _
    "            ""Label"":""Donation"", " & vbNewLine  & _
    "            ""Value"":0.0 " & vbNewLine  & _
    "         }, " & vbNewLine  & _
    "         { " & vbNewLine  & _
    "            ""Id"":""Felt21"", " & vbNewLine  & _
    "            ""Label"":""WebID"", " & vbNewLine  & _
    "            ""Value"":0 " & vbNewLine  & _
    "         } " & vbNewLine  & _
    "      ] " & vbNewLine  & _
    "   }, " & vbNewLine  & _
    "   ""WinkasErrorCode"":0, " & vbNewLine  & _
    "   ""WinKasStatus"":0, " & vbNewLine  & _
    "   ""WinKasStatusString"":""Okay"", " & vbNewLine  & _
    "   ""WinKasMessage"":null, " & vbNewLine  & _
    "   ""ApiVersion"":""1.1.888.91"", " & vbNewLine  & _
    "   ""ResponseDateTime"":""2015-12-04T23:23:19"" " & vbNewLine  & _
    "} "
end sub