zoukankan      html  css  js  c++  java
  • WebLoad 解析服务器返回的JSON格式内容

    服务器返回Json格式的响应内容经常是以 String (txt) 形式返回给客户端。客户端需要把 文本形式的内容还原为Json格式以进一步做处理(如,取得返回内容的一个值作为下个请求的一个输入)。这就要用到 一个函数 evel()。 具体做法如下:
     
    Parsing the JSON Response
    1. 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. In  the Building Block, add a function that receives the document.wlSource object and manipulates it to retrieve the statistics. The script is as follows: 
    function evalResponse ( source) {
        json_response = eval("(" + source + ")")
    }
     
    3. Call the evalResponse function to parse the response contents:
    evalResponse(document.wlSource);
     
    ---------------------------------------具体做法是:-------------------------------
     
    1. 把解析JSON 的函数 提取出来 放在一个单独的parseJSON.js文件里,以便重复利用:
    function parseJSON(source){
        jsonResponse = eval("(" + source + ")");
        return jsonResponse;
    }
     
    2. 在脚本里引进 parseJSON.js 文件:
    function InitAgenda()
    {
        wlGlobals.SaveSource = true; 
        IncludeFile("FunctionLib\parseJSON.js");  //IncludeFile 引进的文件是相对脚本wlp文件的位置;即这里的
                                                                      Function文件夹和wlp文件是放在同一个根目录下的
    }
     
    3. 直接引用parseJSON 文件里的函数:
     
    var loginResponse = document.wlSource;
    var loginObj = parseJSON(loginResponse);
    var advisorId = loginObj.AdvisorId;
     
    当然也可以不需要引进外部文件,而是直接把 eval()函数放在脚本里
     
    注:JSON 节点的读取,如果是{}结构则 直接 a.b, 如果是[], 则 c[index].d 
     
    如下面是一个返回的总结构:
    {
        "ReturnVsBenchmark": {}, 
        "RiskReward": {}, 
        "Portfolios": {}, 
        "Information": {}
    }
    

    展开上面Portfolios 的层次结构如下:

    "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"
                        }, 
                        {}, 
                        {}
                    ]
                }, 
                {}, 
                {}
            ]
        }, 

    要取得 "Id"的值"95646f0f-879d-448b-8bf3-c9bcb15bedd0"  ,则读取的脚本如下:

    var clientResponse = document.wlSource;  // 把 返回的内容存放一个变量中
    var clientObj = eval( "(" + clientResponse + ")" );  // 把返回的txt 格式的内容转化为JSON 格式
    var portfolioId1 = clientObj.Portfolios.Sections[2].Rows[0].Id; //Sections[2]取得是Rows 这个对象,在Sections[]里, 每个{}对象都是Sections 对象数组的一个元素
  • 相关阅读:
    Loadrunner的Tuxedo脚本分析,基本流程和基本函数介绍
    安装ArcGIS Server 9.2的一些建议
    .obj,.lib,.dll,.h之间的相互关系
    中国Albers 投影参数设置参考资料
    投影常识
    vc++2005环境中静态调用DLL(隐式)
    设置GDAL_DATA环境变量
    开源代码搜索利器Koders
    更正GDAL_DATA设置一文错误
    2007年的元宵节
  • 原文地址:https://www.cnblogs.com/tomweng/p/4157656.html
Copyright © 2011-2022 走看看