2015-12-04 127 views
0

我正在嘗試將我們的ERP系統集成到我們的網站中。我能夠從系統中獲得一些json響應。但我真的需要一些幫助解析它。解析經典ASP中的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 
+0

關於在經典中使用JSON有幾個問題ASP。我發現它更容易使用(服務器端)Javascript而不是VBScript作爲腳本語言而不是像ASPJSON這樣的類,請參閱我的答案 - http://stackoverflow.com/questions/30538292/asp-json-object-not-a -collection/30546374#30546374 – John

回答

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