give me the vbs code for: [1. http get the response from the url:…
- User
give me the vbs code for: [1. http get the response from the url: https://papi.lenovo.com.cn/stock/getStockInfo.jhtm?ss=694&callback=jQueryJSONP_stock_getStockInfo&proInfos=%5B%7BactivityType%3A0%2C%20productCode%3A1039473%7D%5D&_=1750685229297 2. the response is structured like "jQueryJSONP_stock_getStockInfo([{ "productCode": "1039473", "salesNumber": "0", "serviceAgencyCode": "-999", "serviceAgencyNumber": "0", "activityType": "0", "message": "success", "needCheckSa": false }])" 3. help me get two outputs: a) productCode in integer and b) salesNumber in integer]
- Assistant
Certainly! Below is a VBScript example that:
- Performs an HTTP GET request to the specified URL.
- Extracts the JSON part from the JSONP response.
- Parses the JSON to get
productCodeandsalesNumber. - Outputs these two values as integers.
VBScript Code
Option Explicit Dim url, http, response, jsonText, jsonObject Dim productCode, salesNumber ' URL to request url = "https://papi.lenovo.com.cn/stock/getStockInfo.jhtm?ss=694&callback=jQueryJSONP_stock_getStockInfo&proInfos=%5B%7BactivityType%3A0%2C%20productCode%3A1039473%7D%5D&_=1750685229297" ' Create the XMLHTTP object Set http = CreateObject("MSXML2.XMLHTTP") ' Send GET request http.Open "GET", url, False http.Send ' Get the response text response = http.responseText ' The response is like: ' jQueryJSONP_stock_getStockInfo([{ ' "productCode": "1039473", ' "salesNumber": "0", ' ... ' }]) ' Extract JSON inside the parentheses jsonText = ExtractJSON(response) ' Parse JSON text Set jsonObject = ParseJSON(jsonText) ' Get productCode and salesNumber from the first element of the array If Not jsonObject Is Nothing Then productCode = CInt(jsonObject(0)("productCode")) salesNumber = CInt(jsonObject(0)("salesNumber")) WScript.Echo "productCode: " & productCode WScript.Echo "salesNumber: " & salesNumber Else WScript.Echo "Failed to parse JSON." End If ' --- Functions --- ' Extract JSON substring from JSONP response Function ExtractJSON(str) Dim startPos, endPos startPos = InStr(str, "(") endPos = InStrRev(str, ")") If startPos > 0 And endPos > startPos Then ExtractJSON = Mid(str, startPos + 1, endPos - startPos - 1) Else ExtractJSON = "" End If End Function ' Parse JSON using ScriptControl (requires Windows Script Host) Function ParseJSON(jsonStr) Dim sc, result On Error Resume Next Set sc = CreateObject("MSScriptControl.ScriptControl") sc.Language = "JScript" ' Use eval to parse JSON string into JScript object Set result = sc.Eval("(" & jsonStr & ")") If Err.Number <> 0 Then Set result = Nothing End If On Error GoTo 0 Set ParseJSON = result End Function
Explanation:
- The script uses
MSXML2.XMLHTTPto perform the HTTP GET. - The response is JSONP, so it extracts the JSON inside the parentheses.
- It uses
MSScriptControl.ScriptControlwith JScript to parse the JSON string into an object. - Then it accesses the first element of the array and converts the
productCodeandsalesNumberto integers. - Finally, it prints the results.
Notes:
MSScriptControl.ScriptControlis available on many Windows systems but may not be installed by default on some newer Windows versions.- If you want a pure VBScript JSON parser, it requires more code or external libraries.
- This script should be run with
cscript(command line) to see the output, e.g.:
cscript yourscript.vbsIf you want me to help with a pure VBScript JSON parser or alternative methods, just ask!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.