服务器返回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 对象数组的一个元素

WebLoad 解析服务器返回的JSON格式内容的更多相关文章

  1. WebLoad 解析服务器返回的XML格式内容

    Parsing the XML Response get the root node:  var rootNode = document.wlXmls[0].XMLDocument.documentE ...

  2. javascript解析从服务器返回的json格式数据

    在javascript中我们可以将服务器返回的json格式数据转换成json格式进行使用,如下: 1.服务器返回的json格式数据: 通过response.responseText获得: " ...

  3. ajaxFileUpload上传文件成功后却无法解析服务器返回的json数据

    可能是应该返回内容带了标签,过滤下 var index=data.indexOf("<"); if (index!=-1){ data=data.substring(0,in ...

  4. 在C#中通过使用Newtonsoft.Json库来解析百度地图地理编码(GeoCoder)服务接口返回的Json格式的数据

    百度地图地理编码(GeoCoder)服务接口返回的Json格式的数据,如下所示: http://api.map.baidu.com/geocoding/v3/?address=**省**市**区**路 ...

  5. JS前端取得并解析后台服务器返回的JSON数据的方法

    摘要:主要介绍:使用eval函数解析JSON数据:$.getJSON()方法获得服务器返回的JSON数据 JavaScript eval() 函数 eval(string) 函数可计算某个字符串,并执 ...

  6. [转]android中解析后台返回的json字符串

    普通形式的:服务器端返回的json数据格式如下: {"userbean":{"Uid":"100196","Showname&qu ...

  7. [Swift通天遁地]四、网络和线程-(11)将服务器返回的JSON映射为实例对象

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  8. $.ajax返回的JSON格式的数据后无法执行success的解决方法

    近段时间做项目,在项目使用了ajax技术,遇到了一个奇怪的问题:"$.ajax返回的JSON格式的数据无法执行success",代码是这样写的: 1 $.ajax({ 2 .. 3 ...

  9. 接口返回数据Json格式处理

    有这样一个页面 , 用来显示用户的账户记录数据,并且需要显示每个月的 收入 支出合计 ,在分页的时候涉及到一些问题,需要对返回的Json格式做处理,处理起来比较麻烦,后端返回的Json数据格式形式如下 ...

随机推荐

  1. Matlab调用C程序 分类: Matlab c/c++ 2015-01-06 19:18 464人阅读 评论(0) 收藏

    Matlab是矩阵语言,如果运算可以用矩阵实现,其运算速度非常快.但若运算中涉及到大量循环,Matlab的速度令人难以忍受的.当必须使用for循环且找不到对应的矩阵运算来等效时,可以将耗时长的函数用C ...

  2. Android Dialogs(4)Dialog事件处理

    Passing Events Back to the Dialog's Host When the user touches one of the dialog's action buttons or ...

  3. js中判断数据类型的方法 typeof

    <input type="text" onblur="demo(this)"/><br/> <input type="n ...

  4. 基于CentOS6.5下如何正确安装和使用Tcpreplay来重放数据(图文详解)

    前期博客 基于CentOS6.5下snort+barnyard2+base的入侵检测系统的搭建(图文详解)(博主推荐) tcpreplay是什么? 简单的说, tcpreplay是一种pcap包的重放 ...

  5. Linux 之 2>&1

    我们在Linux下经常会碰到nohup command>/dev/null 2>&1 &这样形式的命令.首先我们把这条命令大概分解下首先就是一个nohup表示当前用户和系统 ...

  6. 学习笔记 第六章 使用CSS美化图片

    第六章  使用CSS美化图片 6.1  在网页中插入图片 GIF图像 跨平台能力,无兼容性问题: 具有减少颜色显示数目而极度压缩文件的能力,不会降低图像的品质(无损压缩): 支持背景透明功能,便于图像 ...

  7. Win10 1803更新UWP无法安装的解决办法|错误代码0x80073D0D

    升级Win10 1803后,出现了之前安装的UWP.应用无法更新,再此安装失败的现象. 应用商店错误代码为:0x80073D0D,尝试卸载重装商店,清除应用缓存也无法解决. 最终解决办法: 下载Eve ...

  8. JavaScript——responseType

    https://www.cnblogs.com/cdemo/p/5225848.html https://blog.csdn.net/wkyseo/article/details/78232485 异 ...

  9. Node.js——环境变量

  10. (转)淘淘商城系列——zookeeper单机版安装

    http://blog.csdn.net/yerenyuan_pku/article/details/72717744 这篇文章,我单独来说一下zookeeper如何安装.在实际开发中,zookeep ...