The response content returned by the server in JSON format is often returned to the client in String (TXT) Form. The client needs to revert the textual content to JSON format for further processing (for example, to get a value for the returned content as an input to the next request). This will use a function evel (). The following are the specific practices:
parsing the JSON Response1. In the Initagenda () function in the Agenda, define the global variable values of the Savesource property as follows :
function Initagenda ()
{
Wlglobals.savesource = True
}
This instructs Webload to store the complete HTML source code downloaded in the Document.wlsource object. 2. Drag the JavaScript object Building Block from the Toolbox to the Agenda Tree. InchThe Building Block, add a function that receives the Document.wlsource object and manipulates it to retrieve the statistic S. The script is as follows:function Evalresponse (Source) {json_response = eval ("(" + Source + ")")} 3.Call the evalresponse function to the parse the response contents:Evalresponse (Document.wlsource);---------------------------------------Specific practice is to:------------------------------- 1. Extract the function that parses the JSON and put it in a separate parsejson.js file in order to reuse it:function Parsejson (source) {jsonresponse = eval ("(" + Source + ")");return jsonresponse;} 2. Introduce the Parsejson.js file in the script:function Initagenda (){Wlglobals.savesource = true;includefile ("Functionlib\\parsejson.js"); //includefile introduced files are relative to the location of the script WLP file;The function folder and the WLP file are placed under the same root directory} 3. Directly referencing the functions in the Parsejson file: var loginresponse = Document.wlsource;var loginobj = Parsejson (loginresponse);var advisorid = Loginobj.advisorid; It is also possible to put the eval () function directly into the script without having to introduce an external file. Note: The JSON node reads, if the {} structure is directly a.b, if [], then C[INDEX].D The following is a return total structure:
{ "Returnvsbenchmark": {}, "Riskreward": {}, " portfolios": {}, "information": {}}
Expand the hierarchy of the above portfolios as follows:
"Portfolios": { "Id": "Portfolios", "Label": "Portfolios", "Columns": [], "Sections": [ {}, {}, { "Rows": [ { "Id": "95646f0f-879d-448b-8bf3-c9bcb15bedd0", "Status": "Actual", "Oneyear": 0.13786034, "Threeyear": 0.14883958, "Fiveyear": 0.13716583, "Tenyear": 0.11452207, "Trailingasofdate": "2014-10-31", "Marketvalue": 124809.13719108, "Marketvaluecurrencycode": "USD", "Threeyearstddev": 10.08655991, "Threeyearmean": 0.14883958, "Riskrewardasofdate": "2014-10-31", "AccountName": "Portfolio 1", "YTD": 0.09898687, "Returndate": "2014-10-31" }, {}, {} ] }, {}, {} ] },
To get the "Id" value "95646f0f-879d-448b-8bf3-c9bcb15bedd0", read the following script:
var clientresponse = Document.wlsource; //Store the returned content in a variablevar clientobj = eval ("(" + Clientresponse + ")"); //Convert the contents of the returned TXT format into JSON formatvar portfolioId1 = clientobj.portfolios.sections[2]. Rows[0]. Id; //sections[2] Gets the rows of this object, in sections[], each {} object is an element of an array of Sections objects
JSON-formatted content returned by the Webload parsing server