2017-04-01 127 views
0

我需要解析從VBScript中的API返回的JSON。我有將請求發送到API並獲取響應的代碼(可以提供此代碼)。我只需要解析API返回的JSON記錄並使其可用的代碼。我需要知道JSON中每個字段的內容,以及每個記錄何時開始和結束,以便每個記錄可以存儲在數據庫中(不需要數據庫存儲代碼)。使用VBScript循環瀏覽JSON

的JSON模式模式是像這樣:

{ 
    "name": string 
    "id": int 
    "birth": date 
} 

如果你有自己的JSON解析庫,它可以是有用的。我正在使用this(可以提供)。

我到目前爲止的代碼是這樣的:

Set objHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP") 
objHTTP.open "GET", "http://localhost/website/api.asp", False 
'Some headers: 
objHTTP.setRequestHeader "CustomerAccountId", "{my_account_id}" 
objHTTP.setRequestHeader "CustomerId", "{my_customer_id}" 
objHTTP.setRequestHeader "DeveloperToken", "{my_API_access_Key}" 
objHTTP.setRequestHeader "Password", "{my_password}" 
objHTTP.setRequestHeader "UserName", "{my_user_name}" 
objHTTP.send "" 

'Error treatment: 
If objHTTP.Status >= 400 And objHTTP.Status <= 599 Then 
    Response.write "Error Occurred : " & objHTTP.status & " - " & objHTTP.statusText 
    Response.end 
Else 
    Response.write "Success : " & objHTTP.status & " - " & objHTTP.ResponseText 
    Response.end 
End If 

str = objHTTP.responseText 
Set o = json.Decode(str) 
'I need the rest of the code here! 
+0

[parser](http://demon.tw/my-work/vbs-json.html)返回對象或數組。對於這種情況,我想這是數組(我沒有看到整個JSON)。你有沒有嘗試過'For Each ... Next'來遍歷數組元素? – omegastripes

+0

除了「可以爲我寫代碼嗎?」之外,我沒有在這裏看到一個問題,這將是無關緊要的。對於非工作代碼的幫助:顯示非工作代碼並解釋什麼不工作。包含所有您收到的錯誤消息。另外,在普通的VBScript中沒有'response.write'。這是ASP嗎? –

回答

0

看來你使用的是經典的ASP。這意味着您可以輕鬆運行JScr​​ipt而不是VBScript。

意味着你可以使用a proper JSON parser和簡單的語法來訪問結果對象。

<script runat="server" language="JScript" src="json2.js"> 
<script runat="server" language="JScript"> 

function httpGet(url) { 
    var objHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP"); 
    objHTTP.open("GET", url, false); 

    objHTTP.setRequestHeader("CustomerAccountId", "{my_account_id}"); 
    objHTTP.setRequestHeader("CustomerId", "{my_customer_id}"); 
    objHTTP.setRequestHeader("DeveloperToken", "{my_API_access_Key}"); 
    objHTTP.setRequestHeader("Password", "{my_password}"); 
    objHTTP.setRequestHeader("UserName", "{my_user_name}"); 
    objHTTP.send(); 

    // Error treatment 
    if (objHTTP.Status >= 400 && objHTTP.Status <= 599) { 
    Response.write("Error Occurred : " + objHTTP.status + " - " + objHTTP.statusText); 
    Response.end(); 
    } 
    return objHTTP; 
} 

var xhr = httpGet("http://localhost/website/api.asp"); 
var data = JSON.parse(xhr.responseText); 

// read about how to access objects and arrays in JS 
</script> 

甚至可以在同一頁面上混合使用VBScript和Javascript。

<script runat="server" language="VBScript"> 
Function HttpGet(url) 
    Set objHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP") 

    With objHTTP 
     .open "GET", url, False 

     'Some headers: 
     .setRequestHeader "CustomerAccountId", "{my_account_id}" 
     .setRequestHeader "CustomerId", "{my_customer_id}" 
     .setRequestHeader "DeveloperToken", "{my_API_access_Key}" 
     .setRequestHeader "Password", "{my_password}" 
     .setRequestHeader "UserName", "{my_user_name}" 
     .send "" 

     'Error treatment: 
     If .Status >= 400 And .Status <= 599 Then 
      Response.write "Error Occurred : " & .status & " - " & .statusText 
      Response.end 
     End If 
    End With 

    Set HttpGet = objHTTP 
End Function 
</script> 

<script runat="server" language="JScript" src="json2.js"> 
<script runat="server" language="JScript"> 
var xhr = HttpGet("http://localhost/website/api.asp"); 
var data = JSON.parse(xhr.responseText); 

// read about how to access objects and arrays in JS 
</script> 

我沒有展示如何使用JS中的對象。首先,我不知道你的對象結構。另一方面,這是基本的東西,你可以閱讀關於絕對到處。